On Wednesday, June 5, 2019 at 4:31:05 PM UTC-4, Elyse Cottrell-Martin wrote:
I have a lag sequence that I would like to make into a macro since I am potentially going to be using it on about 12 different variables.
This is an example of the syntax I want to make a macro:
COMPUTE var_lag = lag(var).
EXECUTE.
formats var_lag(f5.0).
DO IF (AssessType=0).
RECODE var_lag (ELSE=999).
END IF.
EXECUTE.
RECODE var_lag (999=SYSMIS).
EXECUTE.
I have looked all over the internet but am not clear if I can even do it (I would need to set "var" in the above syntax as something I can define,
So if I say var = Assess4 it would run it as:
COMPUTE Assess4_lag = lag(Assess4 ).
EXECUTE.
formats Assess4_lag(f5.0).
DO IF (AssessType=0).
RECODE Assess4_lag (ELSE=999).
END IF.
EXECUTE.
RECODE Assess4_lag (999=SYSMIS).
EXECUTE.
I tried the following:
****.
DEFINE lagging (var = !cmdend)
COMPUTE var_lag = lag(var).
EXECUTE.
formats var_lag(f5.0).
DO IF (AssessType=0).
RECODE var_lag (ELSE=999).
END IF.
EXECUTE.
RECODE var_lag (999=SYSMIS).
EXECUTE.
!ENDDEFINE.
lagging var = Assess4
****.
But I get a whole bunch of errors (Basically anywhere the var is). I've tried it with !var and !cmdend and I just get the same errors.
This is my first time using Macros (and I just started learning syntax a few weeks ago, although I've used SPSS for a few years). Any help is appreciated.
Rich may be right in suggesting that you don't need a macro. But given the time you've put in on it, you might like to see how to get it working. Try this:
NEW FILE.
DATASET CLOSE ALL.
DATA LIST FREE / Assess4 AssessType (2F1).
BEGIN DATA
1 1 2 1 3 1 4 1
1 0 2 0 3 0 4 0
END DATA.
DEFINE !lagging (var = !cmdend)
!LET !vlag = !CONCAT(!var,"_lag")
COMPUTE !vlag = lag(!var).
formats !vlag(f5.0).
DO IF (AssessType=0).
RECODE !vlag (ELSE=999).
MISSING VALUES !vlag(999).
END IF.
EXECUTE.
!ENDDEFINE.
* Comment out the SET commands when you know everything is working okay.
SET MPRINT ON.
!lagging var = Assess4.
SET MPRINT OFF.
LIST.
OUTPUT from LIST:
Assess4 AssessType Assess4_lag
1 1 .
2 1 1
3 1 2
4 1 3
1 0 999
2 0 999
3 0 999
4 0 999
Number of cases read: 8 Number of cases listed: 8
Notice that I got rid of your recode of 999 into SYSMIS. That is generally a bad idea. See many posts on the topic by Art Kendall in the SPSSX-L mailing list forum (
http://spssx-discussion.1045642.n5.nabble.com/). Instead, I used a MISSING VALUES
command to treat 999 as missing.
Given that you have a lot of variables you want to apply this macro to, the next thing to experiment with would be handing the macro a list of variables, and then making it loop through them. Take a look here for some hints:
https://www.ibm.com/support/knowledgecenter/en/SSLVMB_25.0.0/statistics_reference_project_ddita/spss/base/syn_define_list-processing_loop.html#syn_define_list-processing_loop
HTH.
HTH.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)