<?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: Macro To Create Indicator Variables Fails For Numerical Categorical Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/391656#M94125</link>
    <description>I appreciate the additional clarification.&lt;BR /&gt;&lt;BR /&gt;I was aware that macro logic must be used in a macro and data step logic must be used in a data step, but I didn't know that the two wouldn't play nicely with each other.&lt;BR /&gt;&lt;BR /&gt;Any recommended reading on the topic? I'm sure there are additional nuances that I've overlooked in my limited learning.</description>
    <pubDate>Tue, 29 Aug 2017 20:05:34 GMT</pubDate>
    <dc:creator>ImSpartacus</dc:creator>
    <dc:date>2017-08-29T20:05:34Z</dc:date>
    <item>
      <title>Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390750#M93775</link>
      <description>&lt;P&gt;I&amp;nbsp;am attempting to modify a macro shared &lt;A href="https://communities.sas.com/t5/SAS-Enterprise-Guide/Creating-dummy-variables-for-multiple-categories-in-a-variable/m-p/284903#M19505" target="_blank"&gt;here&lt;/A&gt;. I wanted to concatenate a brief string to the front of each indicator variable's header to mark that it's an indicator and what kind of indicator it is.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is some test data and the modified macro. It appears to work great on character categorical variables such as "Category1" below, but it doesn't work properly on numerical categorical variables such as "Category2" below (all of the indicators have values "1" for all records). Any tips/ideas are appreciated.&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 class=" language-sas"&gt;* Import example dummy data ;

DATA TestData0;
	LENGTH Category1 $ 9;
	INPUT ID Category1 Category2;
	DATALINES;
1 CategoryA 1
2 CategoryB 1
3 CategoryA 1
4 CategoryA 2
5 CategoryC 3
6 CategoryB 2
;
RUN;

* Write macro to generate indicators from categorical variables ;

%macro cat(indata,outdata,variable,abbr);

*Scrub special characters and ensure the categorical variable entries are properly formatted;

DATA CatTest;
	SET &amp;amp;indata.;

	tempvar = "IND_"||"&amp;amp;abbr."||SUBSTR(compress(&amp;amp;variable.,,'kad'),1,MIN(33-LENGTH("IND_")-%LENGTH("&amp;amp;abbr."),LENGTH(compress(&amp;amp;variable.,,'kad')))) ;

RUN;

* Actually generate the indicators ;

  proc sql noprint;
   select distinct tempvar 
    into :mvals separated by '|'
    from CatTest;
    %let mdim=&amp;amp;sqlobs;
  quit;

  * %PUT &amp;amp;mvals; * Log variable string for debugging ;

  data &amp;amp;outdata.(DROP=tempvar);
    set CatTest;
    %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 tempvar = "&amp;amp;_v." then &amp;amp;_v. = 1;
         else &amp;amp;_v = 0;
      end;
      else do;
         if tempvar = &amp;amp;_v. then &amp;amp;_v. = 1;
         else &amp;amp;_v = 0;
            end;
    %end;
  run;

%mend;

* Run macro ;

%CAT(TestData0,TestData1,Category1,CAT1_);
%CAT(TestData1,TestData2,Category2,CAT2_);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Also, as I am still learning SAS, any general tips or suggestions on this macro are certainly welcome.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 20:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390750#M93775</guid>
      <dc:creator>ImSpartacus</dc:creator>
      <dc:date>2017-08-24T20:29:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390770#M93781</link>
      <description>&lt;P&gt;Regardless of the solution, you can probably get along just fine without these variables.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Sort your data set by CATEGORY2, and run a procedure BY CATEGORY2.&amp;nbsp; Or,&lt;/LI&gt;
&lt;LI&gt;Use CATEGORY2 in a CLASS statement (either to process each value separately, or to automatically create and utilize dummy variables such as you are trying to create ... the exact handling depends on the procedure that contains the CLASS statement).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 21:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390770#M93781</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-08-24T21:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390771#M93782</link>
      <description>&lt;P&gt;This may be helpful. Notice at the end there are links to several other methods. IMO I would recommend one of these approaches. Macro's are great, but save them for when absolutely necessary. A large proportion of problems can be solved in multiple ways.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 21:32:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390771#M93782</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-08-24T21:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390781#M93784</link>
      <description>&lt;P&gt;You probably do NOT want to be using macro code to do this problem. &amp;nbsp;But if you did then you need to make sure not to mix your macro code and the SAS code that it is generating. &amp;nbsp;For starters you should add a datastep to determine if the variable is numeric or character and then you can use that value with macro logic to generate different code for numeric an character input variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set &amp;amp;indata ;
  call symputx('vtype',vtype(&amp;amp;variable));
  stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now later you could conditionally generate the code to generate the names for the dummy varaibles based on whether your input variable is character or numeric.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA CatTest;
  SET &amp;amp;indata.;
  length tempvar $32 ;
%if (&amp;amp;vtype=C) %then %do;
  tempvar = "IND_&amp;amp;abbr" || compress(&amp;amp;variable,,'kad');
%end;
%else %do;
  tempvar = "IND_&amp;amp;abbr" || compress(put(&amp;amp;variable,best%eval(32-%length(IND_&amp;amp;abbr)).),,'kad');
%end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that once you have generated the TEMPVAR you know it is character (since it is the name of the dummy varaible) so your last data step can be much simplier as it no longer depends on the type of the original variable. Also you can take advantage of the fact that SAS evaluates boolean expressions to 0 or 1 to indicate false or true, respectively.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;outdata (drop=tempvar);
  set CatTest;
%do _i=1 %to &amp;amp;mdim;
  %let _v = %scan(&amp;amp;mvals,&amp;amp;_i,|);
  &amp;amp;_v = (tempvar="&amp;amp;_v") ;
%end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2017 22:13:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390781#M93784</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-24T22:13:52Z</dc:date>
    </item>
    <item>
      <title>Re: Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390797#M93786</link>
      <description>I greatly appreciate your feedback (haven't tested, but it appears that this is probably how I should've structured it from the start).&lt;BR /&gt;&lt;BR /&gt;One quick question: Could you elaborate on the "you need to make sure not to mix your macro code and the SAS code that it is generating" comment?</description>
      <pubDate>Fri, 25 Aug 2017 00:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390797#M93786</guid>
      <dc:creator>ImSpartacus</dc:creator>
      <dc:date>2017-08-25T00:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390801#M93788</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148548"&gt;@ImSpartacus&lt;/a&gt; wrote:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;One quick question: Could you elaborate on the "you need to make sure not to mix your macro code and the SAS code that it is generating" comment?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In this case this issue is that within a single data step a variable cannot both be character and numeric. Based on how you first reference it SAS will pick one when compiling the data step. &amp;nbsp;So to be able to create a data step that can handle the variable being either character or numeric then you need to use macro logic&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;vtype=C %then %do;
 &amp;lt; code that treats X as character&amp;gt;
%end;
%else %do;
 &amp;lt; code that treats X as character&amp;gt;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and not data step logic.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if vtype(x)='C' then do;
 &amp;lt; code that treats X as character &amp;gt;
end;
else do;
 &amp;lt; code that treats X as numeric &amp;gt;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Aug 2017 01:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/390801#M93788</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-25T01:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro To Create Indicator Variables Fails For Numerical Categorical Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/391656#M94125</link>
      <description>I appreciate the additional clarification.&lt;BR /&gt;&lt;BR /&gt;I was aware that macro logic must be used in a macro and data step logic must be used in a data step, but I didn't know that the two wouldn't play nicely with each other.&lt;BR /&gt;&lt;BR /&gt;Any recommended reading on the topic? I'm sure there are additional nuances that I've overlooked in my limited learning.</description>
      <pubDate>Tue, 29 Aug 2017 20:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-To-Create-Indicator-Variables-Fails-For-Numerical/m-p/391656#M94125</guid>
      <dc:creator>ImSpartacus</dc:creator>
      <dc:date>2017-08-29T20:05:34Z</dc:date>
    </item>
  </channel>
</rss>

