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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.