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 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.
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.
DEFINE !DoLagList (VarList !CMDEND)
!DO !Var !IN (!VarList)
!Let !LagVar= !CONCAT(!Var,'_Lag')
COMPUTE !LagVar = LAG(!Var).
FORMATS !LagVar(f5.0).
IF (AssessType EQ 0) !LagVar=999.
MISSING VALUES !LagVar (999).
VALUE LABELS !LagVar 999 'Missing due to AssessType EQ 0'.
!DOEND
EXECUTE.
!ENDDEFINE.
!DoLagList VarList=x1 x2 x3 …..xK.
Best to reserve SYSMIS for 'system' missing, not for known issues.
On Thursday, June 6, 2019 at 3:34:16 PM UTC-4, David Marso wrote:
DEFINE !DoLagList (VarList !CMDEND)
!DO !Var !IN (!VarList)
!Let !LagVar= !CONCAT(!Var,'_Lag')
COMPUTE !LagVar = LAG(!Var).
FORMATS !LagVar(f5.0).
IF (AssessType EQ 0) !LagVar=999.
MISSING VALUES !LagVar (999).
VALUE LABELS !LagVar 999 'Missing due to AssessType EQ 0'.
!DOEND
EXECUTE.
!ENDDEFINE.
!DoLagList VarList=x1 x2 x3 …..xK.
Best to reserve SYSMIS for 'system' missing, not for known issues.
Hey David, I thought I was taking a page out of ~your~ book when I left that as an exercise for the OP! ;-)
On Thursday, June 6, 2019 at 4:06:15 PM UTC-4, Bruce Weaver wrote:
On Thursday, June 6, 2019 at 3:34:16 PM UTC-4, David Marso wrote:
DEFINE !DoLagList (VarList !CMDEND)
!DO !Var !IN (!VarList)
!Let !LagVar= !CONCAT(!Var,'_Lag')
COMPUTE !LagVar = LAG(!Var).
FORMATS !LagVar(f5.0).
IF (AssessType EQ 0) !LagVar=999.
MISSING VALUES !LagVar (999).
VALUE LABELS !LagVar 999 'Missing due to AssessType EQ 0'.
!DOEND
EXECUTE.
!ENDDEFINE.
!DoLagList VarList=x1 x2 x3 …..xK.
Best to reserve SYSMIS for 'system' missing, not for known issues.
Hey David, I thought I was taking a page out of ~your~ book when I left that as an exercise for the OP! ;-)
I really appreciate all the help... and since I'm new at syntax and macros I appreciate the resources which I am going to go over and learn from.
You actually got a call this afternoon Bruce (or a google chat maybe) and now we aren't even using lag anymore for our project haha. :)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 286 |
Nodes: | 16 (2 / 14) |
Uptime: | 90:19:12 |
Calls: | 6,496 |
Calls today: | 7 |
Files: | 12,100 |
Messages: | 5,277,560 |