Hi, I posted a question earlier that was answered by @PaigeMiller , and I got a solution.
However, this new data set has a twist. Some variables have letters attached to them.
The purpose of this is that I was given raw data, and I'm creating an output that condenses information into one variable.
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 zzinc2a="x" and zzexc2b="x" and zzexc3="x" then CRIT=I2A, E2B , E3 ** new twist**
New data with a twist:
data have2;
infile datalines dsd dlm=",";
input subject $ zzinc1 $ zzinc2a $ zzinc2b $ zzinc2c $ zzinc3 $ zzexc1 $ zzexc2a $ zzexc2b $ zzexc3 $;
datalines;
001, x, x, , x, , , x, ,
002, , x, , , x, , , ,
003, , x, , x, , , , ,
004, , , , , x, , , ,x
;
run;
desired output:
subject CRIT
001 I1, I2A, I2C, E2A
002 I2A, I3
003 I2A, I2C,
004 I3, E3
@PaigeMiller 's solution below for part1
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;
keep subject crit;
run;
link to part1: https://communities.sas.com/t5/SAS-Programming/How-do-I-turn-variables-into-values-for-a-new-variabl...
data want2;
set have2;
length crit $ 200;
array zzi zzinc:;
array zze zzexc:;
crit=' ';
do i=1 to dim(zzi);
if zzi(i)='x' then do;
varname=vname(zzi(i));
crit=cats(crit,', I',substr(varname,6));
end;
end;
do i=1 to dim(zze);
if zze(i)='x' then do;
varname=vname(zze(i));
crit=cats(crit,', E',substr(varname,6));
end;
end;
if crit=:',' then crit=substr(crit,2);
drop i varname;
keep subject crit;
run;
data want2;
set have2;
length crit $ 200;
array zzi zzinc:;
array zze zzexc:;
crit=' ';
do i=1 to dim(zzi);
if zzi(i)='x' then do;
varname=vname(zzi(i));
crit=cats(crit,', I',substr(varname,6));
end;
end;
do i=1 to dim(zze);
if zze(i)='x' then do;
varname=vname(zze(i));
crit=cats(crit,', E',substr(varname,6));
end;
end;
if crit=:',' then crit=substr(crit,2);
drop i varname;
keep subject crit;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.