BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JMagenta
Obsidian | Level 7

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: 

JMagenta_0-1678479521006.png

But What I WANT:

JMagenta_1-1678479569603.png

How can I achieve my goal? Please help.

 

Thank you in advance,

J

 

1 ACCEPTED SOLUTION
5 REPLIES 5
russt_sas
SAS Employee

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;

russt_sas
SAS Employee

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;

JMagenta
Obsidian | Level 7

This kind of worked, only I am now missing some data somehow. I also have duplicates.

 

Thank you,

J

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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 lock in 2025 pricing—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
  • 5 replies
  • 1672 views
  • 1 like
  • 3 in conversation