<?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 Re: Combining and recoding the variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876109#M346170</link>
    <description>&lt;P&gt;Put in a data step and find out&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data users;
   input id mu idu;
datalines;
1105 0 0
1106 0 1
1107 1 0
1108 1 1
1109 . .
1110 . 1
1111 1 .
;
run;


data newcode; 
    set users;
    if mu = 1 and idu = 0 then du = 1; *Marijuana use;  
    if mu = 0 and idu = 1 then du = 2; *non marijuana illegal drugs use;
    if mu = 1 and idu = 1 then du = 3; *both drugs use; 
    if mu = 1 and idu = . then du = 1; *Marijuana use;
    if mu = . and idu = 1 then du = 2; *non marijuana illegal drugs use;
    if mu = 0 and idu = 0 then du = 0; *never use;
    if mu = 0 and idu = . then du = 0; *never use;
    if mu = . and idu = 0 then du = 0; *never use;
    label du = Drug Use;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;results in&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 192pt;" border="0" width="256" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;id&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;mu&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;idu&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;du&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1105&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1106&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1107&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1108&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1109&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1110&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1111&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which looks right.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also go&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newcode2;
	set users;
	du = 0;
	if mu = 1 then do;
		if idu =1 then du = 3;
		else du =1;
	end;
	if idu = 1 then do;
		if mu = 1 then du = 3;
		else du =2;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;just for fun, although I'm not sure it is an improvement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would get in the habit of setting a variable like drugUse2 to zero as a default when you create it so every record is populated.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 May 2023 21:23:44 GMT</pubDate>
    <dc:creator>HB</dc:creator>
    <dc:date>2023-05-16T21:23:44Z</dc:date>
    <item>
      <title>Combining and recoding the variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/875896#M346159</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I have 2 variables: marijuana_Use2 and illegal_drugUse2 both are binary variable.&lt;BR /&gt;I want to combine the variables and name as drugUse2, recode as:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0: Never use drug&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1: marijuana use&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2: Illegal drug use&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3: Both drug use&lt;BR /&gt;I used the code mention below. &lt;SPAN&gt;I'm able to run the code but I am not sure the outcome I obtain is accurate.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;data myproject7; &lt;BR /&gt;    set myproject6;&lt;BR /&gt;    if marijuana_Use2 = 1 and illegal_drugUse2 = 0 then drugUse2 = 1; *Marijuana use;  &lt;BR /&gt;    if marijuana_Use2 = 0 and illegal_drugUse2 = 1 then drugUse2 = 2; *non marijuana illegal drugs use;&lt;BR /&gt;    if marijuana_Use2 = 1 and illegal_drugUse2 = 1 then drugUse2 = 3; *both drugs use; &lt;BR /&gt;    if marijuana_Use2 = 1 and illegal_drugUse2 = . then drugUse2 = 1; *Marijuana use;&lt;BR /&gt;    if marijuana_Use2 = . and illegal_drugUse2 = 1 then drugUse2 = 2; *non marijuana illegal drugs use;&lt;BR /&gt;    if marijuana_Use2 = 0 and illegal_drugUse2 = 0 then drugUse2 = 0; *never use;&lt;BR /&gt;    if marijuana_Use2 = 0 and illegal_drugUse2 = . then drugUse2 = 0; *never use;&lt;BR /&gt;    if marijuana_Use2 = . and illegal_drugUse2 = 0 then drugUse2 = 0; *never use;&lt;BR /&gt;    label drugUse2 = Drug Use;&lt;BR /&gt;run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2023 22:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/875896#M346159</guid>
      <dc:creator>Dawa93</dc:creator>
      <dc:date>2023-05-15T22:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and recoding the variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876109#M346170</link>
      <description>&lt;P&gt;Put in a data step and find out&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data users;
   input id mu idu;
datalines;
1105 0 0
1106 0 1
1107 1 0
1108 1 1
1109 . .
1110 . 1
1111 1 .
;
run;


data newcode; 
    set users;
    if mu = 1 and idu = 0 then du = 1; *Marijuana use;  
    if mu = 0 and idu = 1 then du = 2; *non marijuana illegal drugs use;
    if mu = 1 and idu = 1 then du = 3; *both drugs use; 
    if mu = 1 and idu = . then du = 1; *Marijuana use;
    if mu = . and idu = 1 then du = 2; *non marijuana illegal drugs use;
    if mu = 0 and idu = 0 then du = 0; *never use;
    if mu = 0 and idu = . then du = 0; *never use;
    if mu = . and idu = 0 then du = 0; *never use;
    label du = Drug Use;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;results in&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 192pt;" border="0" width="256" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;id&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;mu&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;idu&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;du&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1105&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1106&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1107&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;0&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1108&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1109&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1110&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1111&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;TD align="right"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which looks right.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also go&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newcode2;
	set users;
	du = 0;
	if mu = 1 then do;
		if idu =1 then du = 3;
		else du =1;
	end;
	if idu = 1 then do;
		if mu = 1 then du = 3;
		else du =2;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;just for fun, although I'm not sure it is an improvement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would get in the habit of setting a variable like drugUse2 to zero as a default when you create it so every record is populated.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 21:23:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876109#M346170</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2023-05-16T21:23:44Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and recoding the variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876113#M346173</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 16 May 2023 22:01:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876113#M346173</guid>
      <dc:creator>Dawa93</dc:creator>
      <dc:date>2023-05-16T22:01:24Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and recoding the variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876148#M346191</link>
      <description>The calculations can be done in one statement.  Try:&lt;BR /&gt;&lt;BR /&gt;druguse2 = max(0, marijuanause2)&lt;BR /&gt;+ 2*max(0, illegal_druguse2);</description>
      <pubDate>Wed, 17 May 2023 02:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876148#M346191</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-05-17T02:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and recoding the variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876157#M346193</link>
      <description>&lt;P&gt;Why not&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;druguse2=sum(0,marijuana_use2) + 2*sum(0,illegal_drug_use2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM function with zero as one of its arguments as above will produce a zero if the other arguments are missing.&lt;/P&gt;</description>
      <pubDate>Wed, 17 May 2023 03:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876157#M346193</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-05-17T03:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: Combining and recoding the variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876751#M346356</link>
      <description>&lt;P&gt;One more (untested) variation came to mind:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;druguse2 = ^^ marijuanause2 + 2 * ^^illegal_druguse2;&lt;/P&gt;</description>
      <pubDate>Sat, 20 May 2023 12:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combining-and-recoding-the-variables/m-p/876751#M346356</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-05-20T12:56:18Z</dc:date>
    </item>
  </channel>
</rss>

