Hi, I'm trying to use the variables in this data as values for a new variable i'm trying to create.
There are 4 subjects, and the "x" represents the value that i want presented in a new variable called CRIT.
So if zzinc3="x", then CRIT=I3.
If zzinc3="x", zzinc10="x", and zzexc7="x" then CRIT=I3, I10, E7
data have;
infile datalines dsd dlm=",";
input subject $ zzinc1 $ zzinc2 $ zzinc3 $ zzinc4 $ zzinc5 $ zzinc6 $ zzinc7 $ zzinc8 $ zzinc9 $ zzinc10 $ zzinc11 $ zzexc1 $ zzexc2 $ zzexc3 $ zzexc4 $ zzexc5 $ zzexc6 $ zzex7 $;
datalines;
001, , , x, , , , , , , x, x, , , , , x, x,
002, x, , , , x, x, , , , , , , x, , , x, ,
003, , x, x, , , , , , , , , , , , x, , ,
004, , , , , , , , , , , , , , , , , ,
;
run;
Desired output:
subject CRIT
001 I3, I10, I11, E5, E6
002 I1, I5, I6, E2, E5
003 I2, I3, E4
004
I ask my usual question ... why are you doing this, what can you do with data in CRIT that you can't do with data in the original form? Often, people think that converting the data to a new form makes things easier, but I am always skeptical. It might be easier, or it might not be, depending on the next step, and you didn't tell us what happens next.
Anyway, here is code to convert the data
data want;
set have;
length crit $ 200;
array zzi zzinc:;
array zze zzexc:;
crit=' ';
do i=1 to dim(zzi);
if zzi(i)='x' then crit=cats(crit,',I',i);
end;
do i=1 to dim(zze);
if zze(i)='x' then crit=cats(crit,',E',i);
end;
if crit=:',' then crit=substr(crit,2);
drop i;
run;
I ask my usual question ... why are you doing this, what can you do with data in CRIT that you can't do with data in the original form? Often, people think that converting the data to a new form makes things easier, but I am always skeptical. It might be easier, or it might not be, depending on the next step, and you didn't tell us what happens next.
Anyway, here is code to convert the data
data want;
set have;
length crit $ 200;
array zzi zzinc:;
array zze zzexc:;
crit=' ';
do i=1 to dim(zzi);
if zzi(i)='x' then crit=cats(crit,',I',i);
end;
do i=1 to dim(zze);
if zze(i)='x' then crit=cats(crit,',E',i);
end;
if crit=:',' then crit=substr(crit,2);
drop i;
run;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.