<?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: Collapsing many variables into one. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598319#M172545</link>
    <description>&lt;P&gt;If your existing variables are boolean (1 for true, 0 or missing for false), do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;asian = kor or bur or lal or cam;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 22 Oct 2019 05:40:29 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-10-22T05:40:29Z</dc:date>
    <item>
      <title>Collapsing many variables into one.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598310#M172541</link>
      <description>My IRB wants me to collapse my racial data for participant privacy but I am struggling to do so. Am I on the right track below, if I'm trying to take Cambodian, Laotian, Korean and Burmese and collapse into Asian?&lt;BR /&gt;&lt;BR /&gt;Data newrace&lt;BR /&gt;Set partrace&lt;BR /&gt;&lt;BR /&gt;Asian= if Kor=1 or Bur=1 or Lal=1 or Cam=1 then Asian.&lt;BR /&gt;&lt;BR /&gt;Thanks!!!</description>
      <pubDate>Tue, 22 Oct 2019 04:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598310#M172541</guid>
      <dc:creator>klongway</dc:creator>
      <dc:date>2019-10-22T04:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing many variables into one.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598317#M172544</link>
      <description>&lt;P&gt;Similar to what you tried:&lt;BR /&gt;&lt;BR /&gt;if kor=1 or bur=1 or lal=1 or cam=1 then asian=1;&lt;BR /&gt;&lt;BR /&gt;A bit different but worth considering:&lt;BR /&gt;&lt;BR /&gt;Asian = max(Kor, bur, cam, lal) ;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 13:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598317#M172544</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-10-22T13:29:17Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing many variables into one.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598319#M172545</link>
      <description>&lt;P&gt;If your existing variables are boolean (1 for true, 0 or missing for false), do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;asian = kor or bur or lal or cam;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Oct 2019 05:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598319#M172545</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-22T05:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing many variables into one.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598325#M172548</link>
      <description>&lt;P&gt;Sample code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   kor=1;
   bur=0;
   lal=0;
   cam=0;
run;

data want1;
   format asian;
   set have;
   if kor=1 or bur=1 or lal=1 or cam=1 then asian=1;
run;

data want2;
   format asian;
   set have;
   asian=(kor=1 or bur=1 or lal=1 or cam=1);
run;

data want3;
   format asian;
   set have;
   asian=(kor or bur or lal or cam);
run;

data want4;
   format asian;
   set have;
   asian=max(kor,bur,lal,cam);
run;

data want5;
   format asian;
   set have;
   asian=max(kor,bur,lal,cam) ne 0;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Explanation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In SAS, False=0, and True=anything except 0, missing, or special missing (I'll explain this below).&lt;/P&gt;
&lt;P&gt;However, typically we use true boolean, i.e. False=0, True=1.&lt;/P&gt;
&lt;P&gt;A comparison operator (in this case = ) returns a boolean value, False=0, True=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Details:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;want1:&lt;/U&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here we are using an IF statement, which returns a boolean value - an IF statement is either True or False.&lt;/P&gt;
&lt;P&gt;Within the IF statement, we are using a specific comparison operator (=), which returns a boolean value based on that comparison.&lt;/P&gt;
&lt;P&gt;We are using the OR operator to "connect" our boolean comparisons, and the OR operator "says" "if any comparison is True, the statement is True".&lt;/P&gt;
&lt;P&gt;We then use an assignment statement to set asian=1 if the statement is true.&lt;/P&gt;
&lt;P&gt;Otherwise, since asian is not retained, it will be set to missing on each iteration of the data step, and thus will remain missing if the IF statement is False.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;want2:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;But remember that the comparison operator returns True, and the OR operator connects the comparisons to return True or False for the entire statement.&lt;/P&gt;
&lt;P&gt;Also remember that SAS returns False=0, True=1.&lt;/P&gt;
&lt;P&gt;So you can put the comparison on the right side of the equals sign - you don't have to use an If statement.&lt;/P&gt;
&lt;P&gt;The asian variable will then capture the results (0 or 1) of the comparison.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;want3:&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Again remember that 0 or missing=False and anything else=True.&lt;/P&gt;
&lt;P&gt;So, we don't even need the comparison operator to evaluate True or False.&lt;/P&gt;
&lt;P&gt;However, notice the difference in the asian variable if you set say Kor=5 or Kor=-5.&lt;/P&gt;
&lt;P&gt;In want3, asian will still be 1 if Kor is any value other than 0, missing, or special missing.&lt;/P&gt;
&lt;P&gt;This may, or may not, be what you want.&amp;nbsp; Should asian=1 if Kor=-5?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;want4:&lt;/P&gt;
&lt;P&gt;The max function will return the maximum value of all the supplied variables.&lt;/P&gt;
&lt;P&gt;So if all values are 0, the max is 0, otherwise the max is returned.&lt;/P&gt;
&lt;P&gt;This is likely what you want if your source variables are boolean (0 or 1 only).&lt;/P&gt;
&lt;P&gt;Otherwise, it may not be what you want.&amp;nbsp; For example, if Kor=-5 ("True") and all other variables=0, the max is 0.&lt;/P&gt;
&lt;P&gt;Test this with say Kor=5 or Kor=-5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;want5:&lt;/P&gt;
&lt;P&gt;But you may want asian to be exactly 0 or 1.&amp;nbsp; If so, use a comparison operator, in this case not equal.&lt;/P&gt;
&lt;P&gt;Note that you can't use != in place of ne since the statement is on the right of the equals sign.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you get different results with the various approaches if your source variables are not boolean.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your flag variables are exactly 0 or 1 (I'll also begrudgingly allow missing but I don't like it), then any of these approaches will give similar results.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I use want3 (comparison on the right side of the equals sign) all the time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In summary:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   length result $5;
   result=ifc(.z,'True','False');put result=;   
   result=ifc(.a,'True','False');put result=;   
   result=ifc(. ,'True','False');put result=;   
   put '======';
   result=ifc(-123,'True','False');put result=;   
   result=ifc(-1,  'True','False');put result=;   
   result=ifc(1,   'True','False');put result=;   
   result=ifc(123, 'True','False');put result=;   
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps...&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 06:31:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598325#M172548</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-10-22T06:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: Collapsing many variables into one.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598344#M172551</link>
      <description>&lt;P&gt;Why do you have so many variables for racial data? Is it possible, that more than variable is 1 at all?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Having just one variable for racial data, would allow to use a format to &lt;EM&gt;change&lt;/EM&gt; the value when displayed. The value is, of course, not changed, but another text&amp;nbsp; will be displayed.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 08:41:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapsing-many-variables-into-one/m-p/598344#M172551</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-10-22T08:41:54Z</dc:date>
    </item>
  </channel>
</rss>

