I need help in figuring out how to output a Subject that has two or more Tobacco types = 1 and create a unique record for each Tobacco type
data example;
input subject cigarettes cigars pipe vape;
datalines;
1001 1 1 0 0
1002 0 1 1 0
1003 0 1 1 1
;
My desired output is
subject | cigarettes | cigars | pipe | vape |
1001 | 1 |
|
|
|
1001 |
| 1 |
|
|
1002 |
| 1 |
|
|
1002 |
|
| 1 |
|
1003 |
| 1 |
|
|
1003 |
|
| 1 |
|
1003 |
|
|
| 1 |
Seems kind of a silly thing to do. But you could do it easily with two transposes.
data example;
input subject cigarettes cigars pipe vape;
datalines;
1001 1 1 0 0
1002 0 1 1 0
1003 0 1 1 1
;
proc transpose data=example out=tall;
by subject;
var cigarettes cigars pipe vape;
run;
proc transpose data=tall out=want(drop=_name_);
where col1>0;
by subject _name_;
id _name_;
var col1;
run;
Obs subject cigarettes cigars pipe vape 1 1001 1 . . . 2 1001 . 1 . . 3 1002 . 1 . . 4 1002 . . 1 . 5 1003 . 1 . . 6 1003 . . 1 . 7 1003 . . . 1
Seems kind of a silly thing to do. But you could do it easily with two transposes.
data example;
input subject cigarettes cigars pipe vape;
datalines;
1001 1 1 0 0
1002 0 1 1 0
1003 0 1 1 1
;
proc transpose data=example out=tall;
by subject;
var cigarettes cigars pipe vape;
run;
proc transpose data=tall out=want(drop=_name_);
where col1>0;
by subject _name_;
id _name_;
var col1;
run;
Obs subject cigarettes cigars pipe vape 1 1001 1 . . . 2 1001 . 1 . . 3 1002 . 1 . . 4 1002 . . 1 . 5 1003 . 1 . . 6 1003 . . 1 . 7 1003 . . . 1
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.