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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.