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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.