Hi,
I have a dataset which has multiple different variables which have the same values across the variables. It would look like this for example:
Obs | Var1 | Var2 | Var3 | Var4 |
1 | A1 | B1 | C1 | D1 |
2 | B1 | D1 | A1 | C1 |
3 | D1 | . | . | . |
4 | C1 | D1 | A1 | . |
I want to create a new variable for each value so that the new variable is a dichotomous yes/no variable but I'm not sure how to code this. For instance, I'd create a variable A1 where 1=yes and 0=no:
Obs | A1 |
1 | 1 |
2 | 1 |
3 | 0 |
4 | 1 |
Thanks in advance for the help!
Assuming there is a relatively short list of possible values for VAR1-VAR4
data want;
set have;
if whichc('A1',of var1-var4) then a1=1;
else a1=0;
/* REPEAT AS NEEDED */
run;
Assuming there is a relatively short list of possible values for VAR1-VAR4
data want;
set have;
if whichc('A1',of var1-var4) then a1=1;
else a1=0;
/* REPEAT AS NEEDED */
run;
data have;
infile cards dlm='09'x truncover;
informat obs 8. var1-var4 $2.;
input obs Var1-Var4;
cards;
1 A1 B1 C1 D1
2 B1 D1 A1 C1
3 D1
4 C1 D1 A1
;
run;
proc transpose data=have out=long;
by obs;
var var1-var4;
run;
data long2;
set long;
where not missing(col1);
value=1;
run;
proc sort data=long2;
by obs col1;
run;
proc transpose data=long2 out=wide;
by obs;
id col1;
var value;
run;
Transposing data tutorials:
Long to Wide:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/
https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/
Wide to Long:
https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-wide-to-long-using-proc-transpose/
https://stats.idre.ucla.edu/sas/modules/reshaping-data-wide-to-long-using-a-data-step/
And sometimes a double transpose is needed for extra wide data sets:
https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: