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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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