Lapis Lazuli | Level 10

## How do I turn variables into values for a new variable? (part 2 with a twist)

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;``````

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: How do I turn variables into values for a new variable? (part 2 with a twist)

``````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;``````
--
Paige Miller
2 REPLIES 2
Diamond | Level 26

## Re: How do I turn variables into values for a new variable? (part 2 with a twist)

``````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;``````
--
Paige Miller
Lapis Lazuli | Level 10