BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

The first data step gives the desired resullt while the second does not.

In second data step why  is  the  variable LastX not retained as it is explicitly mentioned in the retain statment?

 

data initial;
input x $ y;
datalines;
A 2
. 4
. 6
. 8
. 10
B 1 
. 2 
C 1 
. 2 
. 3 
. 4
;
run;

/* Impute missing X values from previous valid values */
data imputed1;
	set initial;
	retain LastX " ";

	if X="" then
		X=LastX;
	else LastX=X;
	drop LastX;
run;

data imputed2;
	set initial;
	retain LastX " ";
	LastX=X;  /* assign the value of LastX */

	if X="" then
		X=LastX;
	drop LastX;
run;

proc print data=imputed1;
run;
proc print data=imputed2;
run 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Retained variables are just like any other variable with the special property that the last value assigned is kept until the start of the next iteration through the data. The value can be changed just like any other variable. So when you use

 

data example;

   set have;

   retain lastx;

   lastx=x;

 

what happens is the value of LastX is that from the previous record up until you reassign it.

 

Similar behaviour occurs if you attempt to Retain a variable that is in the input data set. The value of the variable from incoming record overwrites the retained value.

 

Note that in your exampl ewith XPREVDT that the value is set conditionally: IF First.patient so that assignmet occurs when the first recordd for that patient is read. FIRST. is a very specific type of operation. Note that the example does not have:

   retain XPREVDT;

   XPREVDT = didate;

 

as sequential lines of code.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

The first thing you do in the second data step is assign the value of the CURRENT x to LastX, thereby removing the effect of retaining it.

 

SAS_inquisitive
Lapis Lazuli | Level 10

@ballardw , so assinging value to the retained variables nullify the retaining effect?

I got the following code from a SAS paper (http://support.sas.com/resources/papers/proceedings11/091-2011.pdf).  Variable XPREVDT is assigned  a value (didate) for first patient and still retained for other records for that patient..

 

data diary1;
	set diary1;
	by patient date;
	format XPREVDT date9.;
	retain XPREVDT;

	if first.patient then
		do;
			XPREVDT = didate;
		end;
	else
		do;
			if nmiss(didate - XPREVDT) = 0 then
				lapse = didate - XPREVDT;
			else lapse = .;
			XPREVDT = didate; * reset to current date;
end; run;

  

Also here, LastX is assinged to X in  else statement and it is retained.

 

data imputed1;
	set initial;
	retain LastX " ";

	if X="" then
		X=LastX;
	else LastX=X;
	drop LastX;
run;
Reeza
Super User

@SAS_inquisitive wrote:

@ballardw , so assinging value to the retained variables nullify the retaining effect?

 


Yes, assigning it a value, even missing, overwrites whatever value was in the variable. 

 

The logic below has some flaws beyond the retain issue, which is a danger of user papers. Missing an END for last DO, weird use of NMISS. 

It may also be a copy/paste error, I didn't read reference. Also, different datasets and situations require different logic. Yours may not be the same. 

ballardw
Super User

Retained variables are just like any other variable with the special property that the last value assigned is kept until the start of the next iteration through the data. The value can be changed just like any other variable. So when you use

 

data example;

   set have;

   retain lastx;

   lastx=x;

 

what happens is the value of LastX is that from the previous record up until you reassign it.

 

Similar behaviour occurs if you attempt to Retain a variable that is in the input data set. The value of the variable from incoming record overwrites the retained value.

 

Note that in your exampl ewith XPREVDT that the value is set conditionally: IF First.patient so that assignmet occurs when the first recordd for that patient is read. FIRST. is a very specific type of operation. Note that the example does not have:

   retain XPREVDT;

   XPREVDT = didate;

 

as sequential lines of code.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 3310 views
  • 2 likes
  • 3 in conversation