SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Beanpot
Fluorite | Level 6

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:

ObsVar1Var2Var3Var4
1A1B1C1D1
2B1D1A1C1
3D1...
4C1D1A1.

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:

ObsA1
11
21
30
41

 

Thanks in advance for the help!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Beanpot
Fluorite | Level 6
Thanks this worked great!
Reeza
Super User
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

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Save $200 when you sign up by March 14!

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1491 views
  • 0 likes
  • 3 in conversation