<?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: Create dummy variable across multiple columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474156#M285898</link>
    <description>&lt;P&gt;The diagnosis codes are valid names for variables-- they look like K8001, S722XA, etc. and they would not exceed the limit of 32 characters.&amp;nbsp; They all start with a letter but are a combinations of letters and numbers after that.&amp;nbsp; The spelling will be consistent as well.&amp;nbsp; If it helps, they are ICD-10-CM diagnosis codes.&lt;/P&gt;</description>
    <pubDate>Thu, 28 Jun 2018 16:38:38 GMT</pubDate>
    <dc:creator>0123</dc:creator>
    <dc:date>2018-06-28T16:38:38Z</dc:date>
    <item>
      <title>Create dummy variable across multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474148#M285896</link>
      <description>&lt;P&gt;I'm using SAS University Edition and I need to create dummy variables for my data, but the data I'm using is spread across multiple columns.&amp;nbsp; Each row is a patient's hospital visit.&amp;nbsp; I would like to create a dummy variable for each individual&amp;nbsp;diagnosis code.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's an example of what my data looks like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="have.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21459i8B2383D471BD1DCF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="have.PNG" alt="have.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And here's what I want it to look like in the end:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="want.PNG" style="width: 360px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21460i62B9D0C4EFBE4D13/image-size/medium?v=v2&amp;amp;px=400" role="button" title="want.PNG" alt="want.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another complicating matter is that there are hundreds of Diagnosis columns so I would prefer a way that doesn't require typing them all out.&amp;nbsp; Can anyone offer advice?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 16:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474148#M285896</guid>
      <dc:creator>0123</dc:creator>
      <dc:date>2018-06-28T16:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Create dummy variable across multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474154#M285897</link>
      <description>&lt;P&gt;Hundreds of names isn't a problem.&amp;nbsp; But some other issues are.&amp;nbsp; For example, what do these diagnosis codes look like?&amp;nbsp; Are they valid names for variables in SAS?&amp;nbsp; Perhaps they begin with a number, or perhaps they contain illegal characters such as a decimal point.&amp;nbsp; Or perhaps they contain so many characters that they would exceed SAS's limit of 32 characters for a name.&amp;nbsp; Will the spelling be consistent from one instance to the next?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If they start with a number, how would you like to handle that?&amp;nbsp; Could the word "Diag_" be added&amp;nbsp;as the first part of the name?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 16:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474154#M285897</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-28T16:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create dummy variable across multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474156#M285898</link>
      <description>&lt;P&gt;The diagnosis codes are valid names for variables-- they look like K8001, S722XA, etc. and they would not exceed the limit of 32 characters.&amp;nbsp; They all start with a letter but are a combinations of letters and numbers after that.&amp;nbsp; The spelling will be consistent as well.&amp;nbsp; If it helps, they are ICD-10-CM diagnosis codes.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 16:38:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474156#M285898</guid>
      <dc:creator>0123</dc:creator>
      <dc:date>2018-06-28T16:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create dummy variable across multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474192#M285899</link>
      <description>&lt;P&gt;That's a good start.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To process the data, then, begin by getting lists of all possible diagnosis codes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; create table diag1 as select distinct diagnosis1 as diagnosis from have order by diagnosis;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; create table diag2 as select distinct diagnosis2 as diagnosis from have order by diagnosis;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; create table diag3 as select distinct diagnosis3 as diagnosis from have order by diagnosis;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data all_diags;&lt;/P&gt;
&lt;P&gt;merge diag1 diag2 diag3;&lt;/P&gt;
&lt;P&gt;by diagnosis;&lt;/P&gt;
&lt;P&gt;if diagnosis &amp;gt; ' ';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This gives you a unique list of all codes found in the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next,&amp;nbsp;create a macro variable holding those codes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;select diagnosis into : all_codes separated by ' ' from all_diags;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%put &amp;amp;all_codes;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need the %PUT statement, but it might help illustrate what is happening at this point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, use that list in a DATA step and search for matches.&amp;nbsp; Assuming 6 characters are needed for the longest code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array diags {*} $ 6 &amp;amp;all_codes;&lt;/P&gt;
&lt;P&gt;do _n_ = 1 to dim(diags);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; diags{_n_} = 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if diagnosis1 = vname(diags{_n_}) then diags{_n_} = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if diagnosis2 = vname(diags{_n_}) then diags{_n_} = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if diagnosis3 = vname(diags{_n_}) then diags{_n_} = 1;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm assuming here that you really have just 3 diagnoses per hospital stay.&amp;nbsp; You could always add a few more to the program if need be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is untested.&amp;nbsp; It looks right, so see if it works for&amp;nbsp;what you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jun 2018 17:49:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474192#M285899</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-28T17:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create dummy variable across multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474444#M285900</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id (d1-d3) ($);
cards;
1 A B C
2 Y B D
3 Z B .
;
run;

data temp;
 set have;
 d=d1;v=1;output;
 d=d2;v=1;output;
 d=d3;v=1;output;
run;

proc transpose data=temp(where=(d is not missing)) out=temp1;
by id;
id d;
var v;
run;
proc stdize data=temp1 out=want reponly missing=0;
var _numeric_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jun 2018 14:15:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474444#M285900</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-06-29T14:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create dummy variable across multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474908#M285901</link>
      <description>&lt;P&gt;All of this seems to work up until the last section of code.&amp;nbsp; Then I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2018-07-02 at 9.03.34 AM.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/21533iC8AB30CB98BDCBF0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2018-07-02 at 9.03.34 AM.png" alt="Screen Shot 2018-07-02 at 9.03.34 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know what is wrong or how to get around this error?&lt;/P&gt;</description>
      <pubDate>Mon, 02 Jul 2018 14:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-dummy-variable-across-multiple-columns/m-p/474908#M285901</guid>
      <dc:creator>0123</dc:creator>
      <dc:date>2018-07-02T14:07:54Z</dc:date>
    </item>
  </channel>
</rss>

