BookmarkSubscribeRSS Feed
jag07g
Fluorite | Level 6

I want to combine two variables to create one new variable where group 1 indicates those who use both cigarettes and alcohol (coded 1) and group 2 indicates those who just use either cigarettes or alcohol or use neither (coded 2).

 

The variable ciguse is coded 1 for cigarette use and 2 for no cigarette use. The variable alcoholuse is coded 1 for alcohol use and 2 for no alcohol use. 

 

Does the below code adequately capture everyone in each group and create the intended variable? 

if ciguse=1 and alcoholuse=1 then bothsubstances=1;
*else if ciguse=2 or alcoholuse=2 then bothsubstances=2;
*else bothsubstances=.;
4 REPLIES 4
jag07g
Fluorite | Level 6

Sorry, yes, the asterisks shouldn't be there! 

if cursmk=1 and cursmkls=1 then tobacco2=1;
else if cursmk=2 or cursmkls=2 then tobacco2=2;
else tobacco2=.;
gamotte
Rhodochrosite | Level 12

Hello,

 

Assuming alcoholuse end ciguse can take values 1, 2 and ., here are the possible outcomes.

You can check if it fits your needs.

 

/* Possible values for ...use variables : 1, 2, . */
data usevals;
    input x;
cards;
1
2
.
;
run;

/* We generate all possible combinations of alcoholuse and ciguse */
proc sql noprint;
    CREATE TABLE have AS
    SELECT a.x AS ciguse, b.x AS alcoholuse
    FROM usevals a, usevals b;
quit;

data want;
    set have;
    if ciguse=1 and alcoholuse=1 then bothsubstances=1;
    else if ciguse=2 or alcoholuse=2 then bothsubstances=2;
    else bothsubstances=.;
run;
hashman
Ammonite | Level 13

@jag07g:

One problem is that your in/output values aren't encoded as Booleans, which would be 1 = use and 0 = no use. That would stand to reason seeing that in SAS 1=true and 0=false. This way, the logical expression (sig and alc) would yield true in the case of both sig use and alc use and false otherwise. This is why in order to get what you require you need the IF-THEN-ELSE bunch or, more concisely, the IFN function, such as the expression:

  cig_and_alc = ifN (cig=1 and alc=1, 1, 2) ;

Booleans can still be used; however, due to the odd way the values are encoded, some funny arithmetic has to be added:

data _null_ ;                              
  input cig alc ;                          
  cig_alc = 2 - ((2 - cig) and (2 - alc)) ;
  put cig alc cig_alc= ;                   
  cards ;                                  
1 1                                        
1 2                                        
2 1                                        
2 2                                        
;                                          
run ;                                      

Which yields:

1 1 cig_alc=1
1 2 cig_alc=2
2 1 cig_alc=2
2 2 cig_alc=2

If the values were encoded as normal Booleans, the expression for cig_alc would be much simpler:

data _null_ ;             
  input cig alc ;         
  cig_alc = cig and alc ; 
  put cig alc cig_alc= ;  
  cards ;                 
1 1                       
1 0                       
0 1                       
0 0                       
;                         
run ;                     

and the result would be much more, uh, logical:

1 1 cig_alc=1
1 0 cig_alc=0
0 1 cig_alc=0
0 0 cig_alc=0

Kind regards

Paul D.

 

       

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 506 views
  • 2 likes
  • 4 in conversation