Hi @Kurt_Bremser !
Thank you for your suggestion.
So, you mean I can replace the code of @PeterClemmensen
if first.type then call missing(lags3, lags18, lags29);
to
if first.type then do;
lags3 = . ;
lags18 = . ;
lags29= . ;
end;
Is it what you mean?
Thank you!
@Phil_NZ wrote:
Hi @Kurt_Bremser !
Thank you for your suggestion.
So, you mean I can replace the code of @PeterClemmensenif first.type then call missing(lags3, lags18, lags29);
to
if first.type then do; lags3 = . ; lags18 = . ; lags29= . ; end;
Is it what you mean?
Thank you!
Both code snippets are equivalent, it's a matter of style preferences.
You can simply that code quite a lot. Also your test for missing values is triggering the notes about operations on missing values. Use either the MISSING() or N() function to test for missing values. Test for missing values before testing for a positive result for the calculation.
data argentinalag;
set argentina;
by type;
lags3 = lag(s3);
lags7 = lag(s7);
lags18 = lag(s18);
lags29 = lag(s29);
lags22 = lag(s22);
lags43 = lag(s43);
lagcf_ope_act=lag(cf_ope_act);
if first.type then call missing(of lags:);
if n(s27,s2)=2 then if (1+s27/s2)>0 then do;
cf_ope_act= log(1 + s27/s2);
lagcf_ope_act=lag(cf_ope_act);
if first.type then call missing(lagcf_ope_act);
end;
run;
Note that calling the LAG() function inside the IF/THEN DO/END block means you are lagging the values of cf_ope_act from the previous time it was calculated and NOT form the previous observation in the source dataset. If that is NOT what you want then change the logic to move these two statement after the conditional calculation of cf_ope_act.
if n(s27,s2)=2 then if (1+s27/s2)>0 then do;
cf_ope_act= log(1 + s27/s2);
end;
lagcf_ope_act=lag(cf_ope_act);
if first.type then call missing(lagcf_ope_act);
Here is the impact on making that change:
Variables with Unequal Values
Variable Type Len Ndif MaxDif MissDif
lagcf_ope_act NUM 8 87 0 87
Hi @Tom
Thank you for your insightful suggestion. I understand call missing to assign the missing value for specified variables. However, I do not understand the part "of lags" ?
I just think the code
if first.type then call missing(lagcf_ope_act);
is enough rather than
if first.type then call missing(of lags: lagcf_ope_act);
Please correct me if I explain you wrongly!
Many thanks!
Read about using variable lists in your code. The string lags: means all the variables whose name starts with those four characters that have been defined as of that point in the data step.
In addition to allowing the use of a variable list like lags: the OF keyword in a function call allows you to list variables delimited by space (the same way you would in any regular SAS statement) instead of forcing you to insert commas between the variable names.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.