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

I have the following data

Case_number Company  party_role
1 A Plaintiff
1 B  
1 C Defendant
1 D  
2 E Plaintiff
2 F  
2 G Defendant
2 H  

 

 

I want the output to look like this. 

 

Case_number Company  party_role
1 A Plaintiff
1 B Plaintiff
1 C Defendant
1 D Defendant
2 E Plaintiff
2 F Plaintiff
2 G Defendant
2 H Defendant

 

I tried following code but not working 

 

data want;
set have;
retain repeat1;
if first.case_number then do;
if party_role ne '' then repeat1 = party_role; else do;
if party_role eq '' then party_role = repeat1; end; drop repeat1;
end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data want;
update have(obs=0) have;
by Case_number;
output;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User
data want;
set have;
retain repeat1;
if first.case_number
then do;
  if party_role ne ''
  then repeat1 = party_role;
  else do;
    if party_role eq '' then party_role = repeat1;
  end; 
  drop repeat1;
end;
run;

Do you see the logic mistake?

The log will also alert you to the fact that you tried to use a FIRST. variable without a corresponding BY.

 

BTW, DROP is a declarative statement. Putting it into a conditional branch is at best misleading.

Ksharp
Super User
data want;
update have(obs=0) have;
by Case_number;
output;
run;
Kurt_Bremser
Super User

You probably want this:

data want;
set have;
by case_number;
retain repeat1;
if first.case_number then repeat1 = "";
if party_role ne ""
then repeat1 = party_role;
else party_role = repeat1;
drop repeat1;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1873 views
  • 0 likes
  • 3 in conversation