BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Phil_NZ
Barite | Level 11

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!

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Kurt_Bremser
Super User

@Phil_NZ wrote:

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!


Both code snippets are equivalent, it's a matter of style preferences.

Tom
Super User Tom
Super User

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                                                           
Phil_NZ
Barite | Level 11

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!

Thank you for your help, have a fabulous and productive day! I am a novice today, but someday when I accumulate enough knowledge, I can help others in my capacity.
Tom
Super User Tom
Super User

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.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 19 replies
  • 5036 views
  • 11 likes
  • 4 in conversation