<?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: Use an array to create binary variable from categorical in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678069#M23812</link>
    <description>&lt;P&gt;Make a list of variables, and use a macro to create the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ na_resp na_retract;
datalines;
A 0 2
B 1 1
C 2 0
;

%let varlist=na_resp na_retract;

%macro make_binary(vars);
%do i = 1 %to %sysfunc(countw(&amp;amp;vars));
  %let var = %scan(&amp;amp;vars,&amp;amp;i);
  if &amp;amp;var in (0,1,2)
  then do;
    &amp;amp;var._0=(&amp;amp;var.=0);
    &amp;amp;var._1=(&amp;amp;var.=1);  
    &amp;amp;var._2=(&amp;amp;var.=2);
  end;
%end;
%mend;

data want;
set have;
%make_binary(&amp;amp;varlist);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Aug 2020 12:53:44 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-08-20T12:53:44Z</dc:date>
    <item>
      <title>Use an array to create binary variable from categorical</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678061#M23808</link>
      <description>&lt;P&gt;Trying to determine if there is a more efficient way to accomplish turning a categorical variable with 3 values into 3 binary variables:&lt;/P&gt;
&lt;P&gt;if na_resp in (0,1,2) then&lt;BR /&gt;&amp;nbsp; do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; na_resp_0=(na_resp=0);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; na_resp_1=(na_resp=1);&amp;nbsp;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; na_resp_2=(na_resp=2);&lt;BR /&gt;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have several variables with the same structure and would like to use an array to transform them all, but I can't figure out if it is possible to create the 3 new variables I need automatically from the variables in the array, i.e. I would need to create na_resp_0, na_resp_1, na_resp_2 for the first variable. Is it possible to affix a suffix to an array value? Something like:&lt;BR /&gt;array nurse (*) na_resp na_retract na_cyan na_airent na_grunt;&lt;BR /&gt;&amp;nbsp; do i=1 to dim(nurse);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if nurse(i) in (0,1,2) then&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do j=0 to 2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; nurse(i)_j=(nurse(i)=j);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; end;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 12:10:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678061#M23808</guid>
      <dc:creator>kmorrowvt</dc:creator>
      <dc:date>2020-08-20T12:10:37Z</dc:date>
    </item>
    <item>
      <title>Re: Use an array to create binary variable from categorical</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678069#M23812</link>
      <description>&lt;P&gt;Make a list of variables, and use a macro to create the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $ na_resp na_retract;
datalines;
A 0 2
B 1 1
C 2 0
;

%let varlist=na_resp na_retract;

%macro make_binary(vars);
%do i = 1 %to %sysfunc(countw(&amp;amp;vars));
  %let var = %scan(&amp;amp;vars,&amp;amp;i);
  if &amp;amp;var in (0,1,2)
  then do;
    &amp;amp;var._0=(&amp;amp;var.=0);
    &amp;amp;var._1=(&amp;amp;var.=1);  
    &amp;amp;var._2=(&amp;amp;var.=2);
  end;
%end;
%mend;

data want;
set have;
%make_binary(&amp;amp;varlist);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 12:53:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678069#M23812</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-20T12:53:44Z</dc:date>
    </item>
    <item>
      <title>Re: Use an array to create binary variable from categorical</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678070#M23813</link>
      <description>&lt;P&gt;Thanks, I was trying to mix macros and arrays and having no luck.This should do the trick.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 12:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678070#M23813</guid>
      <dc:creator>kmorrowvt</dc:creator>
      <dc:date>2020-08-20T12:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Use an array to create binary variable from categorical</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678072#M23814</link>
      <description>&lt;P&gt;You can make the code more efficient by replacing the many implicit IFs with a single SELECT statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro make_binary(vars);
%do i = 1 %to %sysfunc(countw(&amp;amp;vars));
  %let var = %scan(&amp;amp;vars,&amp;amp;i);
  select (&amp;amp;var);
  %do j = 0 %to 2;
    when (&amp;amp;j.) &amp;amp;var._&amp;amp;j. = 1;
  %end;
    otherwise;
  end;
%end;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do not like the missing values for the "unset" variables, you can set them to 0 in a separate %DO loop. Unconditional assignments eat considerably less CPU than conditions.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 13:04:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678072#M23814</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-20T13:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: Use an array to create binary variable from categorical</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678425#M23847</link>
      <description>&lt;P&gt;It is generating a design matrix.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp; wrote a blog about it a couple of years ago;&lt;/P&gt;
&lt;PRE&gt;data have;
input id $ na_resp na_retract;
datalines;
A 0 2
B 1 1
C 2 0
;
data temp;
 set have;
 dummy=1;
run;
proc logistic data=temp outdesign=x(drop=dummy) outdesignonly noprint;
class  na_resp na_retract /param=glm;
model dummy=na_resp na_retract/nofit noint;
run;
data want;
 merge have x;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 12:42:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678425#M23847</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-08-21T12:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Use an array to create binary variable from categorical</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678463#M23848</link>
      <description>&lt;P&gt;There is no need to write a program or use a macro. As KSharp says, this is called creating a design matrix, and &lt;A href="https://blogs.sas.com/content/iml/2016/02/24/create-a-design-matrix-in-sas.html" target="_self"&gt;SAS provides many ways to generate a design matrix&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2018/07/30/names-columns-design-matrix.html" target="_self"&gt;If you want the names of the dummy variables to reflect the name and values of the original categorical variables&lt;/A&gt;, I suggest using PROC GLMSELECT, as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* add a fake response variable */
data AddY / view=AddY;
set have;
_Y = 1;
run;

proc glmselect data=AddY NOPRINT
               outdesign(addinputvars)=Want2(drop=_Y);
   class      na_resp na_retract;   /* list the categorical variables here */
   model _Y = na_resp na_retract /  /* and here */
              noint selection=none;
run;

proc print data=Want2; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 14:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Use-an-array-to-create-binary-variable-from-categorical/m-p/678463#M23848</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-08-21T14:51:16Z</dc:date>
    </item>
  </channel>
</rss>

