<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Combining Two Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586394#M167404</link>
    <description>&lt;P&gt;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).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does the below code adequately capture everyone in each group and create the intended variable?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ciguse=1 and alcoholuse=1 then bothsubstances=1;
*else if ciguse=2 or alcoholuse=2 then bothsubstances=2;
*else bothsubstances=.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 Sep 2019 12:18:10 GMT</pubDate>
    <dc:creator>jag07g</dc:creator>
    <dc:date>2019-09-05T12:18:10Z</dc:date>
    <item>
      <title>Combining Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586394#M167404</link>
      <description>&lt;P&gt;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).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does the below code adequately capture everyone in each group and create the intended variable?&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ciguse=1 and alcoholuse=1 then bothsubstances=1;
*else if ciguse=2 or alcoholuse=2 then bothsubstances=2;
*else bothsubstances=.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Sep 2019 12:18:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586394#M167404</guid>
      <dc:creator>jag07g</dc:creator>
      <dc:date>2019-09-05T12:18:10Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586396#M167406</link>
      <description>&lt;P&gt;Remove the comment asterisks and try it. Methinks it will work, but testing beats guessing.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2019 12:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586396#M167406</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-09-05T12:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586405#M167410</link>
      <description>&lt;P&gt;Sorry, yes, the asterisks shouldn't be there!&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if cursmk=1 and cursmkls=1 then tobacco2=1;
else if cursmk=2 or cursmkls=2 then tobacco2=2;
else tobacco2=.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Sep 2019 12:48:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586405#M167410</guid>
      <dc:creator>jag07g</dc:creator>
      <dc:date>2019-09-05T12:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586455#M167414</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming alcoholuse end ciguse can take values 1, 2 and ., here are the possible outcomes.&lt;/P&gt;
&lt;P&gt;You can check if it fits your needs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 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;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Sep 2019 16:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586455#M167414</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-09-05T16:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: Combining Two Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586475#M167422</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214271"&gt;@jag07g&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  cig_and_alc = ifN (cig=1 and alc=1, 1, 2) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Booleans can still be used; however, due to the odd way the values are encoded, some funny arithmetic has to be added:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 ;                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which yields:&lt;/P&gt;
&lt;PRE&gt;1 1 cig_alc=1
1 2 cig_alc=2
2 1 cig_alc=2
2 2 cig_alc=2
&lt;/PRE&gt;
&lt;P&gt;If the values were encoded as normal Booleans, the expression for cig_alc would be much simpler:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 ;                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and the result would be much more, uh, logical:&lt;/P&gt;
&lt;PRE&gt;1 1 cig_alc=1
1 0 cig_alc=0
0 1 cig_alc=0
0 0 cig_alc=0
&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2019 16:53:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-Two-Variables/m-p/586475#M167422</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-05T16:53:47Z</dc:date>
    </item>
  </channel>
</rss>

