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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
RichardDeVen
Barite | Level 11

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.

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You could try it and find out (and let us know what happens).

--
Paige Miller
RichardDeVen
Barite | Level 11

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.

kingCobra
Obsidian | Level 7
Thanks Richard for the detail explanation. I just constructed the example dataset and choose random names for the variables. My idea was to get the theoretical basis for RETAIN statement.
In my case, I was getting 1 more count as the previous subjid has variable1='NOT DONE' as the last record.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 588 views
  • 0 likes
  • 3 in conversation