Hi all,
I have had a theoretical question in mind for long time.
Does the order matter while resetting values in RETAIN statement?
Ex -
proc sort data=have;
by subjid parameter visit;
run;
data want;
retain total 0;
set have;
by subjid parameter visit;
if first.parameter then total=0;
if variable1='NOT DONE' then total=total+1;
run;
Does it produce the same result if I change the order of the statement marked in red?
That is
1. if variable1='NOT DONE' then total=total+1;
2. if first.parameter then total=0;
In some complex cases I think I have seen the resetting statement (if first.parameter then total=0;) at #1 does not actually reset the value of total. But I could not found any theoretical basis for the ordering of the statements.
Any help will be appreciated.
The RETAIN statement is not executable, it is a compile time statement that sets the initial value of the variable.
Example:
In the sample the extra emphasis on compile time behavior is shown by placing the RETAIN statement at the bottom of the step. The first PUT shows the initialization value.
data _null_; put x=; set sashelp.class; retain x 123; run;
For your case of REATAINing a total value that incrementally counts the number of NOT DONEs over a parameter within a subjid, the order of statements absolutely matters. The "total" might be better named or labeled "how_many_not_dones_so_far"
The first. conditional must occur first
if first.parameter then total=0; if variable1='NOT DONE' then total=total+1;
Consider what would happen if the statements were reversed, AND, (edge case) the data had NOT DONE in the first row of the group?
if variable1='NOT DONE' then total=total+1; if first.parameter then total=0;
In the (edge case) your total would be one less that what it should be because the total starting at first.parameter state should be 1, not 0.
You could try it and find out (and let us know what happens).
The RETAIN statement is not executable, it is a compile time statement that sets the initial value of the variable.
Example:
In the sample the extra emphasis on compile time behavior is shown by placing the RETAIN statement at the bottom of the step. The first PUT shows the initialization value.
data _null_; put x=; set sashelp.class; retain x 123; run;
For your case of REATAINing a total value that incrementally counts the number of NOT DONEs over a parameter within a subjid, the order of statements absolutely matters. The "total" might be better named or labeled "how_many_not_dones_so_far"
The first. conditional must occur first
if first.parameter then total=0; if variable1='NOT DONE' then total=total+1;
Consider what would happen if the statements were reversed, AND, (edge case) the data had NOT DONE in the first row of the group?
if variable1='NOT DONE' then total=total+1; if first.parameter then total=0;
In the (edge case) your total would be one less that what it should be because the total starting at first.parameter state should be 1, not 0.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.