BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
gpv2000
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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
Reeza
Super User
Agree with Tom, this data structure does not make logical sense. What are you attempting to that makes you think you require this data structure?

You mention, "output a Subject that has two or more Tobacco types = 1" but your sample data does not include any observations that meet that criteria.
gpv2000
Calcite | Level 5
I probably did not express clearly. Cigarettes, cigars, vape and pipes come under the umbrella of tobacco. If the subject uses any of these, it is captured as 1. I needed to output subjects that has 2 or more tobacco types as 2 records.
Reeza
Super User
The solution you've selected will output regardless if it's 2 or 1.

You can add a WHERE statement to filter it but I still feel this is probably a inefficient way to achieve a goal.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 373 views
  • 2 likes
  • 3 in conversation