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 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 780 views
  • 1 like
  • 3 in conversation