<?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: Making if then statement grab multiple lines from dataset instead of just first one described. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484345#M125755</link>
    <description>I edited my original post to be more clear in what I am working with and what I actually want.</description>
    <pubDate>Mon, 06 Aug 2018 13:10:13 GMT</pubDate>
    <dc:creator>plantprion</dc:creator>
    <dc:date>2018-08-06T13:10:13Z</dc:date>
    <item>
      <title>Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484335#M125750</link>
      <description>&lt;P&gt;I am trying to make a bit of code that will take a string from my dataset in excel and turn it into a number. However, there are some observations that have multiple groups and my code is only grabbing the first one it sees and I would like it to somehow recognize that there are multiple different groups in the same observations and classify it as other. Here is my example:&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;Where I want White to be coded as 1, African American to be coded as 2 etc and if&amp;nbsp;there are two different races then I want&amp;nbsp;6 for other.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code for attempting to do this:&lt;/P&gt;&lt;PRE&gt;data race;
   input ID $ Race $;
   datalines;
1 White 
2 AfricanAmerican &lt;BR /&gt;3 White/AfricanAmerican&lt;BR /&gt;4 Hispanic&lt;BR /&gt;5 Hispanic/AfricanAmerican&lt;BR /&gt;6 NativeAmerican&lt;BR /&gt;7 PacificIslander&lt;BR /&gt;8 Other&lt;BR /&gt;9 PacificIslander/NativeAmerican&lt;BR /&gt;10 Hispanic/White 
;&lt;BR /&gt;&lt;BR /&gt;data race;&lt;BR /&gt;if find (Race, "White") ge 1 then code = 1;&lt;BR /&gt;else if find (Race, "AfricanAmerican") ge 1 then code = 2;&lt;BR /&gt;else if find (Race, "Hispanic") ge 1 then code = 3;&lt;BR /&gt;else if find (Race, "NativeAmerican") ge 1 then code = 4;&lt;BR /&gt;else if find (Race, "PacificIslander") ge 1 then code = 5;&lt;BR /&gt;else if find (Race, "Other") ge 1 then code = 6;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 13:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484335#M125750</guid>
      <dc:creator>plantprion</dc:creator>
      <dc:date>2018-08-06T13:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484337#M125751</link>
      <description>&lt;P&gt;This works:&lt;/P&gt;
&lt;PRE&gt;data race;
  length race $200;
  input id $ race $;
  datalines;
1 White 
2 AfricanAmerican 
3 White/AfricanAmerican 
;
run;

data race;
  set race;
  if index(race, "White") then code = 1;
  if index(race, "AfricanAmerican") then code=2;
  if index(race,"White") and index(race,"AfricanAmerican") then code=3;
run;&lt;/PRE&gt;
&lt;P&gt;Am using a couple of simplifications, index returns the first position of the word in the string or 0, so if its found then true.&amp;nbsp; Also I use greatest wins, i.e. White would first become 1, then become 3 later on due to position of the if.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TBH I wouldn't do it this way.&amp;nbsp; First step would be to split out the race value into component parts, e.g.:&lt;/P&gt;
&lt;PRE&gt;array r{2} $200;
do i=1 to countw(race,"/");
  r{i}=scan(race,i,"/");
end;&lt;/PRE&gt;
&lt;P&gt;Then you would apply a format to the races using a proc format.&amp;nbsp; Its just easier to standardise that way.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 12:47:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484337#M125751</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-06T12:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484339#M125752</link>
      <description>&lt;P&gt;Thank you for your help, the issue is that I have many more race variables then just the two so I did not want to just break up the two parts for every possible&amp;nbsp;combination&amp;nbsp;of races. For your second solution, I actually have no idea how that works so if you have any reading or can explain what you are doing in that second step that would be very helpful. Thanks again.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 12:53:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484339#M125752</guid>
      <dc:creator>plantprion</dc:creator>
      <dc:date>2018-08-06T12:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484343#M125754</link>
      <description>&lt;P&gt;Present some accurate test data then, and show what you want the output to look like.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 13:02:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484343#M125754</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-06T13:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484345#M125755</link>
      <description>I edited my original post to be more clear in what I am working with and what I actually want.</description>
      <pubDate>Mon, 06 Aug 2018 13:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484345#M125755</guid>
      <dc:creator>plantprion</dc:creator>
      <dc:date>2018-08-06T13:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484350#M125757</link>
      <description>&lt;P&gt;So:&lt;/P&gt;
&lt;PRE&gt;data race;
  length id race $200;
   input id $ race $;
   datalines;
1 White 
2 AfricanAmerican 
3 White/AfricanAmerican
4 Hispanic
5 Hispanic/AfricanAmerican
6 NativeAmerican
7 PacificIslander
8 Other
9 PacificIslander/NativeAmerican
10 Hispanic/White 
;
run;

data race;
  set race;
   if index(race,"White") then code = 1;
   if index(race,"AfricanAmerican") then code = 2;
   if index(race,"Hispanic") then code = 3;
   if index(race,"NativeAmerican") then code = 4;
   if index(race,"PacificIslander") then code = 5;
   if index(race,"Other") then code = 6;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Aug 2018 13:20:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484350#M125757</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-08-06T13:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: Making if then statement grab multiple lines from dataset instead of just first one described.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484357#M125758</link>
      <description>&lt;P&gt;So do the combinations belong in code=6, or do they start a new category such as code=7?&amp;nbsp; I'll use 7 for the sample code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Starting with the first ELSE statement, turn each into two possibilities.&amp;nbsp; I'll illustrate just one set of changes for one ELSE statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;else if find (Race, "AfricanAmerican") then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if code=. then code=2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else code=7;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So each time you find a match, check whether CODE was already assigned by an earlier match.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 13:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Making-if-then-statement-grab-multiple-lines-from-dataset/m-p/484357#M125758</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-08-06T13:30:01Z</dc:date>
    </item>
  </channel>
</rss>

