## SAS set statement and if clause

Solved
Occasional Contributor
Posts: 5

# SAS set statement and if clause

data central;
input patient visit datatype \$;
cards;
1 10 cenrtal
1 11 cenrtal
1 12 cenrtal
2 13 cenrtal
2 14 cenrtal
2 15 cenrtal
3 16 cenrtal
3 17 cenrtal
3 18 cenrtal
;
run;
data local;
input patient studyday datatype \$;
cards;
1 100 local
1 121 local
1 144 local
2 169 local
2 196 local
2 225 local
3 256 local
3 289 local
3 324 local
;
run;

data all1;
set local central;
if visit=. then visit=sqrt(studyday);
run;

data all;
set local central;
run;
data all2;
set all;
if visit=. then visit=sqrt(studyday);
run;

Can someone help me? Why the results of all1 and all2 are different? Why in data all1 the visit variable automate retain while in all2 not?

Accepted Solutions
Solution
‎05-31-2018 01:58 PM
Super User
Posts: 6,781

## Re: SAS set statement and if clause

Great question!

Any variable that comes from a SAS data set is automatically retained.  With that in mind, consider what happens on the second observation.

When constructing ALL1, VISIT does come from a SAS data set.  So its value is retained.  On the first observation, VISIT got calculated as being 10.  So on the second observation, that value is retained.  Since VISIT is not missing, it does not get recalculated.

When constructing ALL2, again VISIT comes from a SAS data set.  So again its value is retained.  However, in the process of constructing the data set ALL, VISIT became part of every observation in ALL.  (It has a missing value for those observations that came from LOCAL.)  On the first observation, VISIT gets calculated as being 10.  On the second observation, the result is a little different (as you have seen).  The SET statement reads in the second observation from ALL, where VISIT has a missing value.  That missing value overwrites the retained value of 10.  So VISIT is now missing and the IF THEN condition is true.

It might help to add PUT statements before the SET statement, after the SET statement, and after the IF THEN statement to examine the value of VISIT at each point along the way.

All Replies
Solution
‎05-31-2018 01:58 PM
Super User
Posts: 6,781

## Re: SAS set statement and if clause

Great question!

Any variable that comes from a SAS data set is automatically retained.  With that in mind, consider what happens on the second observation.

When constructing ALL1, VISIT does come from a SAS data set.  So its value is retained.  On the first observation, VISIT got calculated as being 10.  So on the second observation, that value is retained.  Since VISIT is not missing, it does not get recalculated.

When constructing ALL2, again VISIT comes from a SAS data set.  So again its value is retained.  However, in the process of constructing the data set ALL, VISIT became part of every observation in ALL.  (It has a missing value for those observations that came from LOCAL.)  On the first observation, VISIT gets calculated as being 10.  On the second observation, the result is a little different (as you have seen).  The SET statement reads in the second observation from ALL, where VISIT has a missing value.  That missing value overwrites the retained value of 10.  So VISIT is now missing and the IF THEN condition is true.

It might help to add PUT statements before the SET statement, after the SET statement, and after the IF THEN statement to examine the value of VISIT at each point along the way.

Occasional Contributor
Posts: 5