## How do I turn variables into values for a new variable?

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

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: How do I turn variables into values for a new variable?

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

## Re: How do I turn variables into values for a new variable?

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

## Re: How do I turn variables into values for a new variable?

Hi PaigeMiller,
This is how the raw data is presented to me, and I'm creating an output that summarizes a list of those variables marked "x", because they represent criterias that were not met for each subject. It is better to present it is a comma-separated list condensed into one variable, rather than all 18 variables be presented. The desired output is the final step.