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

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? 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

 

View solution in original post

2 REPLIES 2
Astounding
PROC Star

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.

 

jeremyyjm
Calcite | Level 5

Thanks, Astounding, great explanation.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 668 views
  • 2 likes
  • 2 in conversation