<?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 Using a loop to create indicator variables from Categorical string Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262543#M51281</link>
    <description>&lt;P&gt;I need a do loop that will read a categorical variable and then make an indicator variable for each category. Everything I've found on the web relies on the categorical variable being equal to some number which is not the case for me. I do not want to hard code these dummy variables because they will change from one data set to the next and the values will continually be different. Also, the number of categories can be in the 100s.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2673iB33050746357EB6B/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Cat3.JPG" title="Cat3.JPG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2674i5100BAF3E30734BF/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Cat4.JPG" title="Cat4.JPG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All help would be so greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code below is the closest that I have gotten to getting this to work. It just doesn't work because of the numberical problem.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%macro cat(indata, variable);
  proc sql noprint;
    select distinct &amp;amp;variable. into :mvals separated by '|'
    from &amp;amp;indata.;

    %let mdim=&amp;amp;sqlobs;
  quit;

  data &amp;amp;indata.;
    set &amp;amp;indata.;
    %do _i=1 %to &amp;amp;mdim.;
      %let _v = %scan(&amp;amp;mvals., &amp;amp;_i., |);
      if &amp;amp;variable. = &amp;amp;_v. then &amp;amp;variable.&amp;amp;_v. = 1; else &amp;amp;variable.&amp;amp;_v = 0;
    %end;
  run;
%mend;

%cat(people, income);&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Apr 2016 17:36:54 GMT</pubDate>
    <dc:creator>SmcGarrett</dc:creator>
    <dc:date>2016-04-08T17:36:54Z</dc:date>
    <item>
      <title>Using a loop to create indicator variables from Categorical string Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262543#M51281</link>
      <description>&lt;P&gt;I need a do loop that will read a categorical variable and then make an indicator variable for each category. Everything I've found on the web relies on the categorical variable being equal to some number which is not the case for me. I do not want to hard code these dummy variables because they will change from one data set to the next and the values will continually be different. Also, the number of categories can be in the 100s.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2673iB33050746357EB6B/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Cat3.JPG" title="Cat3.JPG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/2674i5100BAF3E30734BF/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Cat4.JPG" title="Cat4.JPG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All help would be so greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code below is the closest that I have gotten to getting this to work. It just doesn't work because of the numberical problem.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%macro cat(indata, variable);
  proc sql noprint;
    select distinct &amp;amp;variable. into :mvals separated by '|'
    from &amp;amp;indata.;

    %let mdim=&amp;amp;sqlobs;
  quit;

  data &amp;amp;indata.;
    set &amp;amp;indata.;
    %do _i=1 %to &amp;amp;mdim.;
      %let _v = %scan(&amp;amp;mvals., &amp;amp;_i., |);
      if &amp;amp;variable. = &amp;amp;_v. then &amp;amp;variable.&amp;amp;_v. = 1; else &amp;amp;variable.&amp;amp;_v = 0;
    %end;
  run;
%mend;

%cat(people, income);&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2016 17:36:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262543#M51281</guid>
      <dc:creator>SmcGarrett</dc:creator>
      <dc:date>2016-04-08T17:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using a loop to create indicator variables from Categorical string Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262553#M51285</link>
      <description>&lt;P&gt;For this approach to work you need to make sure that the length of text resulting from &lt;FONT face="SAS Monospace" size="2"&gt;&amp;amp;&lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;variable.&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&amp;amp;&lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;_v.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;never exceeds&lt;/STRONG&gt; 32 characters. It also may not contain spaces or charcters other than letters, digits and _.&lt;/P&gt;
&lt;P&gt;The function NVALID&amp;nbsp; would allow you validate whether &amp;amp;variable.&amp;amp;_v creates a valid name.&lt;/P&gt;
&lt;P&gt;You may also run into problems creating a variable name that already exists in your dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the VTYPE function to execute code conditionally. You have an issue with how character and numeric variables would behave.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am very leery of using:&lt;/P&gt;
&lt;P&gt;Data &amp;amp;indata;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Set &amp;amp;indata;&lt;/P&gt;
&lt;P&gt;construct in this context as there is a potential for destroying existing data if the &amp;amp;variable.&amp;amp;_v duplicates an existing variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code addresses the Character/Numeric issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro cat(indata, variable);
  proc sql noprint;
    select distinct &amp;amp;variable. into :mvals separated by '|'
    from &amp;amp;indata.;

    %let mdim=&amp;amp;sqlobs;
  quit;

  data &amp;amp;indata.;
    set &amp;amp;indata.;
    %do _i=1 %to &amp;amp;mdim.;
      %let _v = %scan(&amp;amp;mvals., &amp;amp;_i., |);
      if VType(&amp;amp;variable)='C' then do;
         if &amp;amp;variable. = "&amp;amp;_v." then &amp;amp;variable.&amp;amp;_v. = 1;
         else &amp;amp;variable.&amp;amp;_v = 0;
      end;
      Else do;
         if &amp;amp;variable. = &amp;amp;_v. then &amp;amp;variable.&amp;amp;_v. = 1;
         else &amp;amp;variable.&amp;amp;_v = 0;
      end;
    %end;
  run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Apr 2016 18:16:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262553#M51285</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-08T18:16:39Z</dc:date>
    </item>
    <item>
      <title>Re: Using a loop to create indicator variables from Categorical string Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262556#M51286</link>
      <description>&lt;P&gt;SInce you didin't posted any executable indata code, I'll just to paper examine your code.&lt;/P&gt;
&lt;P&gt;What&amp;nbsp;do you mean by numberical problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From what I see in the data step, I think you shouldn't use &amp;amp;Variable. in the assignments, just &amp;amp;_v, no?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whenever your are trouble shooting macros, examine the logs after setting symbolgen, mprint ant potentially mlogic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know this is a foo example to simplify your post, but the data structure in your output makes little sense. I&amp;nbsp;guess that you have a proper id variable somewhere. Either way, why keeping the&amp;nbsp;original category var? I guess is a way to&amp;nbsp;transpose your data for sttaistical/mining like analysis - but then you usually condense no of rows, which you&amp;nbsp;can't if you keep your category var.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2016 18:24:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262556#M51286</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-04-08T18:24:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using a loop to create indicator variables from Categorical string Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262561#M51289</link>
      <description>&lt;P&gt;SAS has tools to make this easy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transreg design data=sashelp.class;
   model class(name / zero=none cprefix=0);
   output out=design;
   run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Apr 2016 18:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262561#M51289</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-04-08T18:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using a loop to create indicator variables from Categorical string Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262596#M51306</link>
      <description>&lt;P&gt;THANK YOU THANK YOU THANK YOU!&lt;/P&gt;</description>
      <pubDate>Fri, 08 Apr 2016 21:13:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-loop-to-create-indicator-variables-from-Categorical/m-p/262596#M51306</guid>
      <dc:creator>SmcGarrett</dc:creator>
      <dc:date>2016-04-08T21:13:59Z</dc:date>
    </item>
  </channel>
</rss>

