I have a data set of patients and diagnosis codes, for example
name, code
Sarah, A1
Sarah, B1
Sarah, C1
Matt, A2
Matt, E2
Dave, A2
Dave, B3
Dave, E2
George, C2
George, A1
I am screening the patients for a research study. They all have A1 or A2. If they also have a B code or C code, they are not eligible for the study. I want to create a set like the following that just has 1 entry for each patient with a new variable that says if they are eligible and the code variable should be their A code.
name, code, eligible
Sarah, A1, no
Matt, A2, yes
Dave, A2, no
George, A1, no
How can I look at each code and then create one observation with a new variable to say if the patient is eligible?
Assuming the data is grouped by name:
data want;
set have;
by name notsorted;
length eligible 8 a_code $ 2;
retain eligible a_code;
if first.name then do;
eligible = 1;
end;
if first(code) = 'A' then do;
a_code = code;
end;
eligible = eligible and (first(code) not in ('B', 'C'));
if last.name then do;
code = a_code;
output;
end;
run;
data have; infile cards dlm=' ,'; input name $ code $; cards; Sarah, A1 Sarah, B1 Sarah, C1 Matt, A2 Matt, E2 Dave, A2 Dave, B3 Dave, E2 George, C2 George, A1 ; proc sql; create table want as select name,min(code) as code, ifc(sum(code eqt 'B' or code eqt 'C'),'no ','yes' ) as eligible from have group by name; quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.