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;
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.