<?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: how to use macro and loop to create multiple dummy variables for glm in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278729#M56090</link>
    <description>&lt;P&gt;Thank you. I want to use those as my confounding factors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;For example, for race, only black is related and will be considered as a confounding.&lt;/P&gt;&lt;P&gt;For group, one group II, will be considered as my confounding.&lt;/P&gt;&lt;P&gt;So my final model will be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Y=aX+ g(2)+ race(b)&lt;/P&gt;&lt;P&gt;So I do not want to add too many that are not related to my model&lt;/P&gt;&lt;P&gt;any advice?&lt;/P&gt;</description>
    <pubDate>Mon, 20 Jun 2016 16:43:44 GMT</pubDate>
    <dc:creator>Bal23</dc:creator>
    <dc:date>2016-06-20T16:43:44Z</dc:date>
    <item>
      <title>how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278715#M56085</link>
      <description>&lt;P&gt;I need to create multiple dummy variables, for age, I need to create 3 dummy varibles, for group, 4, for race, 2, for edu, 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I am thinking to use a loop macro way&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro glan(demog);
%let var1 = group;
	%let var2 = race;
%let var3=edu;

data want;
  set have;
  array edu(3) edu1-edu3;
  do i = 1 to 3;
    edu(i)=(educat=i);
  end;
drop i;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I guess I can use nested loop but not sure how to do it. The problem is, each variable has different catogories and need different numbers of dummy variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice to make use of the array structure to make our coding more efficient? Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 15:34:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278715#M56085</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-06-20T15:34:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278724#M56088</link>
      <description>&lt;P&gt;Why would you create dummy variables at all?&amp;nbsp; GLM can handle this for you, with the CLASS statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_glm_sect011.htm" target="_blank"&gt;https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_glm_sect011.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 16:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278724#M56088</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-20T16:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278729#M56090</link>
      <description>&lt;P&gt;Thank you. I want to use those as my confounding factors.&lt;/P&gt;&lt;P&gt;&amp;nbsp;For example, for race, only black is related and will be considered as a confounding.&lt;/P&gt;&lt;P&gt;For group, one group II, will be considered as my confounding.&lt;/P&gt;&lt;P&gt;So my final model will be&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Y=aX+ g(2)+ race(b)&lt;/P&gt;&lt;P&gt;So I do not want to add too many that are not related to my model&lt;/P&gt;&lt;P&gt;any advice?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 16:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278729#M56090</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-06-20T16:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278737#M56096</link>
      <description>&lt;P&gt;If you really only need a handful of them, just hard-code them:&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;dummy1 = (group=2);&lt;/P&gt;
&lt;P&gt;dummy2 = (race='black');&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't have to create all possible variables, just those that you will need in your equation.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 16:53:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278737#M56096</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-20T16:53:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278739#M56097</link>
      <description>&lt;P&gt;Thanks. This is one solution, but not exactly what I want. I need to do multiple analysis, so those dummy variables can be generated&amp;nbsp;for other analyses, not only limited to glm. There are different models. I really want to try the nested loop and macro. Or, if only one, either nested loop, or macro,&amp;nbsp;can work, that will be also great.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 17:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278739#M56097</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-06-20T17:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278741#M56099</link>
      <description>&lt;P&gt;Here are a few considerations, then.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have any long variable names?&amp;nbsp; It would be convenient to name the dummy variables after the originals, such as race_1, race_2, race_3, etc.&amp;nbsp; But that can fail if the original variable names are too long.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do the values of any variables contain more than one word?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could any variables have a missing value?&amp;nbsp; If so, should a dummy be assigned for the missing values?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 17:07:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278741#M56099</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-20T17:07:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278752#M56102</link>
      <description>&lt;P&gt;The variable names some times are as long as 7 letters&lt;/P&gt;&lt;P&gt;yes, some values contain more than one word, like "HS diploma"&lt;/P&gt;&lt;P&gt;yes, some contain missing values. Missing values can be ommited, since that is a very small number.&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 17:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278752#M56102</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-06-20T17:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278757#M56104</link>
      <description>&lt;P&gt;Why don't you just use PROC GLMMOD to do it for you?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 17:37:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278757#M56104</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-06-20T17:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278758#M56105</link>
      <description>&lt;P&gt;One last question.&amp;nbsp; Is it reasonable to ask you to call a macro with a separate list of character vs. numeric variables?&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%make_dummies (charlist=edu, numlist=age race)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is too much work, you would still need to somehow indicate which variables to use for creating dummies, and which to omit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, a macro will find all possible values for your variables, create the dummy variables, and label them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if Tom is suggesting GLMMOD, I would definitely look into that ... very credible source!&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 17:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278758#M56105</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-20T17:41:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278763#M56107</link>
      <description>&lt;P&gt;&lt;STRONG&gt;yes&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%make_dummies (charlist=edu group race, numlist=age )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 17:49:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278763#M56107</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-06-20T17:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278774#M56110</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let varlist=age sex ;
%let dsin=sashelp.class;
%let dsout=want ;

data step1 ;
  _id +1;
  set &amp;amp;dsin (keep=&amp;amp;varlist);
run;

proc glmmod noprint data=step1 outparm=step2 outdesign=step3 ;
  class &amp;amp;varlist;
  model _id=&amp;amp;varlist /noint;
run;

data step4 ;
  set step2;
  old=cats('COL',_colnum_);
  new=cats(of effname &amp;amp;varlist);
run;

proc sql noprint;
  select catx('=',old,new)
    into :rename separated by ' '
    from step4 
  ;
quit;

data &amp;amp;dsout ;
  set &amp;amp;dsin ;
  set step3 (drop=_id  rename=(&amp;amp;rename)) ;
run;




&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Jun 2016 18:09:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278774#M56110</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-06-20T18:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278777#M56112</link>
      <description>&lt;P&gt;OK, this will be a bit clumsy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro make_dummies (charlist=, numlist=);&lt;/P&gt;
&lt;P&gt;%local i j nextvar;&lt;/P&gt;
&lt;P&gt;proc freq data=have noprint;&lt;/P&gt;
&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;charlist &amp;amp;numlist);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %let nextvar = %scan(&amp;amp;charlist &amp;amp;numlist, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; tables &amp;amp;nextvar / out=&amp;amp;nextvar (keep=&amp;amp;nextvar);&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;data _null_;&lt;/P&gt;
&lt;P&gt;call execute ('data want; set have;');&lt;/P&gt;
&lt;P&gt;%if %length(&amp;amp;charlist) %then %do i=1 %to %sysfunc(countw(&amp;amp;charlist));&lt;/P&gt;
&lt;P&gt;%let nextvar = %scan(&amp;amp;charlist, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; counter=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (done&amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;nextvar (where=(&amp;amp;nextvar &amp;gt; ' ')) end=done&amp;amp;i;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; counter + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; call execute(cats("&amp;amp;nextvar._", counter, "= (&amp;amp;nextvar=", &amp;amp;nextvar, ');',&lt;/P&gt;
&lt;P&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; "label &amp;amp;nextvar._", counter, "= '&amp;amp;nextvar';"));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;%** Similar loop for numerics;&lt;/P&gt;
&lt;P&gt;call execute('run;');&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend make_dummies;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's getting complicated enough that I thought I should stop here before writing the loop for numeric variables.&amp;nbsp; It needs to be tested and debugged, and perhaps scrapped if Tom's solution is easier.&amp;nbsp; So it can be tested using character variables only for now.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jun 2016 18:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/278777#M56112</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-20T18:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/279308#M56281</link>
      <description>&lt;P&gt;Thank you very much. It is very long and I am lost. Which should be replaced with my own variables? Which should be kept?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jun 2016 13:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/279308#M56281</guid>
      <dc:creator>Bal23</dc:creator>
      <dc:date>2016-06-22T13:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: how to use macro and loop to create multiple dummy variables for glm</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/279310#M56283</link>
      <description>&lt;P&gt;You would need to put your data into a data set named "have" since that name is hard-coded within the macro.&amp;nbsp; (That part can always be changed later by convertting the data set name into a macro parameter.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then call the macro by naming a few character variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%make_dummies (charlist=name1 name2 name3)&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jun 2016 13:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-use-macro-and-loop-to-create-multiple-dummy-variables-for/m-p/279310#M56283</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-22T13:07:40Z</dc:date>
    </item>
  </channel>
</rss>

