Hello,
I was having trouble using the last value in my groups as the value for all members in my groups. Basically, I want whatever the last value in the group is to be the value for all members in that group.
What I have:
data Have;
input ID Name $;
cards;
1 ''
1 Yes
2 ''
2 No
3 ''
3 ''
3 Yes
4 No
5 ''
5 Yes
6 No
8 Yes
;
run;
Which looks like:
But What I WANT:
How can I achieve my goal? Please help.
Thank you in advance,
J
DOW loop:
data want;
do until (last.id);
set have (rename=(name=_n));
by id;
end;
do until (last.id);
set have;
by id;
name = _n;
output;
end;
drop _n;
run;
Here is one way to accomplish your task:
data Have;
input ID Name $;
cards;
1 .
1 Yes
2 .
2 No
3 .
3 Yes
4 No
5 .
5 .
5 Yes
6 No
8 Yes
;
data new;
set have;
by id;
if last.id then output;
run;
data final;
merge have(in=a) new(rename=(name=name2));
by id;
if a;
run;
data final2;
set final;
retain name2;
by id;
if first.id then new=name2;
if name ne . then new=name;
keep id name2;
run;
proc print;
run;
This is really all you need:
data Have;
input ID Name $;
cards;
1 .
1 Yes
2 .
2 No
3 .
3 Yes
4 No
5 .
5 .
5 Yes
6 No
8 Yes
;
data new;
set have;
by id;
if last.id then output;
run;
data final;
merge have(in=a) new(rename=(name=name2));
by id;
if a;
run;
This kind of worked, only I am now missing some data somehow. I also have duplicates.
Thank you,
J
DOW loop:
data want;
do until (last.id);
set have (rename=(name=_n));
by id;
end;
do until (last.id);
set have;
by id;
name = _n;
output;
end;
drop _n;
run;
Thank you much!
J
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.