Hello,
I wonder if someone could help me? I try to make a table which inherits the group information from previous value. Thank you.
Input:
group | id |
group1 | 1 |
2 | |
3 | |
group2 | 4 |
5 | |
group3 | 6 |
Ouput:
group | id |
group1 | 1 |
group2 | 2 |
group3 | 3 |
group2 | 4 |
group3 | 5 |
group3 | 6 |
Either I don't understand or your example wasn't right. Are you trying to do the following:
data have; infile cards dlm=',' truncover; input group $ id; cards; group1,1 ,2 ,3 group2,4 ,5 group3,6 ; proc sort data=have; by descending id; run; data want (drop=hold); set have; retain hold; if not missing(group) then hold=group; else group=hold; run;
Art, CEO, AnalystFinder.com
Either I don't understand or your example wasn't right. Are you trying to do the following:
data have; infile cards dlm=',' truncover; input group $ id; cards; group1,1 ,2 ,3 group2,4 ,5 group3,6 ; proc sort data=have; by descending id; run; data want (drop=hold); set have; retain hold; if not missing(group) then hold=group; else group=hold; run;
Art, CEO, AnalystFinder.com
Thank you so much. It worked.
You should just be able to use a retained variable.
data want ;
set have;
hold = coalescec(group,hold);
group = coalescec(group,hold);
retain hold;
drop hold;
run;
Unfortunately SAS sees the first reference to HOLD as being the reference inside the function call instead of the result of assignment statement and defaults HOLD to numeric. So you need to add a LENGTH statement to define HOLD.
Or you could use this trick to make a copy of GROUP with the name of HOLD. That way you do not need to know the length of GROUP in advance. It will also mark the variable as retained since it is coming from a dataset, so the RETAIN statement is not needed.
data want ;
set have have (obs=0 rename=(group=hold));
hold = coalescec(group,hold);
group = coalescec(group,hold);
drop hold;
run;
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.
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.