BookmarkSubscribeRSS Feed
rogerward
Obsidian | Level 7

Hi groupies,

I am struggling with this seemingly simple retain statement.

In the first data step (called HAVE), the variable COUNTER is sometimes missing. 

When it is missing, per ID, I want (as shown in the second data step) NEWCOUNTER to be equal to the retained value of COUNTER.  In other words, if COUNTER is missing, I want NEWCOUNTER to be equal to the previous value previous non-missing value of COUNTER.

The following two data steps represent what I HAVE and what I WANT.

Thanks a lot for your help! 

 

data have;
input id counter;
datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
run;

data want;
input id counter newcounter;
datalines;
1 1 1
1 2 2
2 1 1
3 1 1
3 . 1
3 2 2
3 . 2
3 . 2
3 3 3
;
run;

6 REPLIES 6
sbxkoenk
SAS Super FREQ
data have;
input id counter;
rowid=_N_;
datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
run;

proc expand data=have
            out=want
            METHOD=STEP;
  id rowid;
  convert counter = newcounter ;
run;
/* end of program */

PROC EXPAND is a SAS/ETS procedure.
Koen

rogerward
Obsidian | Level 7
Thanks, Koen. I have never used PROC EXPAND. I need to expand my life.
data_null__
Jade | Level 19

The update statement can be used to carry forward the value of counter.   

 

data have;
   input id counter;
   datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
run;

data want;
   if 0 then set have;
   update have(obs=0 keep=id) have(rename=(counter=newcounter));
   by id;
   set have(keep=counter);
   output;
   run;

Capture.PNG

rogerward
Obsidian | Level 7

Thanks a lot, data_null__ ! It is very clever.

FreelanceReinh
Jade | Level 19

Hi @rogerward,

 

You can also use the RETAIN statement:

data want;
set have;
newcounter=ifn(id=lag(id),coalesce(counter,newcounter),counter);
retain newcounter;
run;

 

The code can be simplified if (as in your sample data) the COUNTER of the first observation of an ID is never missing:

data want;
set have;
newcounter=coalesce(counter,newcounter);
retain newcounter;
run;
mkeintz
PROC Star
data have;
input id counter;
datalines;
1 1
1 2
2 1
3 1
3 .
3 2
3 .
3 .
3 3
;
data want (drop=_:);
  set have;
  by id;
  retain _ctr;
  if first.id=1 or counter^=. then _ctr=counter;
  else counter=_ctr; 
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 875 views
  • 6 likes
  • 5 in conversation