<?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: flagging cases in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283800#M57858</link>
    <description>&lt;P&gt;There are many ways to conditionally assign values to another variable. This will usually be done in a datastep.&lt;/P&gt;
&lt;P&gt;The basic approach is IF THEN ELSE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Age='a' then flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if Age='b' then flag=2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if Age='c' then flag=3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if Age='d' then flag=4;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;However this construct can get lengthy if you have many values. If you have a range of values such as a, b, c, d should have a flag of 1 and e, f, g, h should have a flag of 2 you can use an IN operator with a list:&lt;/P&gt;
&lt;P&gt;if Age in ('a' 'b' 'c' 'd') then flag=1;&lt;/P&gt;
&lt;P&gt;else if Age in ('e' 'f' 'g' 'h') then flag=2;&lt;/P&gt;
&lt;P&gt;If your comparison is a range of numeric values such as 1 to 10&lt;/P&gt;
&lt;P&gt;if Age in (1:10) then flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Longer or more complex approaches might be a Select statement.&lt;/P&gt;
&lt;P&gt;Custom formats are also often used especially if multiple variables may have a similar coding need. In some cases formats are perferred because by assigning a format to the existing variable you may not need to add another variable at all for many analysis tasks.&lt;/P&gt;</description>
    <pubDate>Tue, 12 Jul 2016 15:31:35 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-07-12T15:31:35Z</dc:date>
    <item>
      <title>flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283796#M57854</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;This is my first post/question on this site and I am a beginner.&lt;/P&gt;&lt;P&gt;I want to have a flag variable corresponding to a data set such that it changes value as the value of a certain variable changes.&lt;/P&gt;&lt;P&gt;For ex: if variable Age has the values a, b, c, d then variable flag takes the value 1,2,3,4 respectively for each occurence of values a,b,c,d&lt;/P&gt;&lt;P&gt;Please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Yogesh Singh&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 15:20:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283796#M57854</guid>
      <dc:creator>Yogesh2601</dc:creator>
      <dc:date>2016-07-12T15:20:46Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283797#M57855</link>
      <description>&lt;P&gt;This is called recoding a variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use IF/THEN statements&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.ats.ucla.edu/stat/sas/modules/vars.htm" target="_blank"&gt;http://www.ats.ucla.edu/stat/sas/modules/vars.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this will be done multiple times or is really long then you can consider using a format instead.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www2.sas.com/proceedings/sugi30/001-30.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi30/001-30.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 15:25:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283797#M57855</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-12T15:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283799#M57857</link>
      <description>&lt;P&gt;Here are some issues that are important to finding the right solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, are other variables involved?&amp;nbsp; For example, are there multiple records for each patient and the pattern (1, 2, 3, 4) needs to start over again when there is a different patient?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, what about repeats?&amp;nbsp; For example, what should the output look like if the variable in question follows the pattern a, b, a, c so that the variable is changing but one of the values repeats an earlier value?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 15:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283799#M57857</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-12T15:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283800#M57858</link>
      <description>&lt;P&gt;There are many ways to conditionally assign values to another variable. This will usually be done in a datastep.&lt;/P&gt;
&lt;P&gt;The basic approach is IF THEN ELSE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Age='a' then flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if Age='b' then flag=2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if Age='c' then flag=3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if Age='d' then flag=4;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;However this construct can get lengthy if you have many values. If you have a range of values such as a, b, c, d should have a flag of 1 and e, f, g, h should have a flag of 2 you can use an IN operator with a list:&lt;/P&gt;
&lt;P&gt;if Age in ('a' 'b' 'c' 'd') then flag=1;&lt;/P&gt;
&lt;P&gt;else if Age in ('e' 'f' 'g' 'h') then flag=2;&lt;/P&gt;
&lt;P&gt;If your comparison is a range of numeric values such as 1 to 10&lt;/P&gt;
&lt;P&gt;if Age in (1:10) then flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Longer or more complex approaches might be a Select statement.&lt;/P&gt;
&lt;P&gt;Custom formats are also often used especially if multiple variables may have a similar coding need. In some cases formats are perferred because by assigning a format to the existing variable you may not need to add another variable at all for many analysis tasks.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 15:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283800#M57858</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-12T15:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283808#M57862</link>
      <description>I did it with IF/THEN/ELSE but is there any easier way to do it?</description>
      <pubDate>Tue, 12 Jul 2016 16:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283808#M57862</guid>
      <dc:creator>Yogesh2601</dc:creator>
      <dc:date>2016-07-12T16:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283809#M57863</link>
      <description>I did it with IF/ELSE. Is there any easier way to do it?</description>
      <pubDate>Tue, 12 Jul 2016 16:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283809#M57863</guid>
      <dc:creator>Yogesh2601</dc:creator>
      <dc:date>2016-07-12T16:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283810#M57864</link>
      <description>No other variables are involved. And for repeat cases the flage variable should have the same value.&lt;BR /&gt;&lt;BR /&gt;For ex: Age Flag&lt;BR /&gt;11 1&lt;BR /&gt;12 2&lt;BR /&gt;13 3&lt;BR /&gt;14 4&lt;BR /&gt;11 1&lt;BR /&gt;15 5&lt;BR /&gt;&lt;BR /&gt;I did it with IF/THEN/ELSE. Please suggest an alternative solution.</description>
      <pubDate>Tue, 12 Jul 2016 16:07:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283810#M57864</guid>
      <dc:creator>Yogesh2601</dc:creator>
      <dc:date>2016-07-12T16:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283812#M57866</link>
      <description>&lt;P&gt;If you only have 4 values your unlikely to find a solution that's actually less code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The only one I can think of is an array look up,,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Array age_fmt(*) _temporary_ (11:15);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Flag = whichn(age, of age_fmt(*));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, since you seem to have a pattern in your age/flag&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Flag = age - 10;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 16:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283812#M57866</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-12T16:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283825#M57869</link>
      <description>&lt;P&gt;When you have just a few possible values, there's no easier way to do it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you have large numbers of values, there are still many ways to do it.&amp;nbsp; But it's difficult to find a short, simple way.&amp;nbsp; Here's one possible approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;select distinct age into : agelist separated by ' ' from have where age &amp;gt; .;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array ages {&amp;amp;sqlobs} _temporary_ (&amp;amp;agelist);&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;flag = whichn(age, of ages{*});&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that this will not assign a value to FLAG when age is missing.&amp;nbsp; And it requires AGE to be numeric.&amp;nbsp; (There are slightly more complex ways that this approach could be adapted to character variables.)&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jul 2016 16:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283825#M57869</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-12T16:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283840#M57874</link>
      <description>&lt;P&gt;You may want to define "easier". Things to consider in choosing between methods for a task revolve around how easy it may be to understand the code, how robust to modify and sometimes use of resources such as memory or cpu cycles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF-then-else usually satisfies the first for single variables, not so much if you are using multiple varaibles and multiple "ors" and "ands" in the comparisons. It may also be easy to modify when adding&amp;nbsp;on or two more values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A similar structure is the SELECT-when which evaluates an expression a the top and then branches to the specific value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select (age);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('a') flag=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('b') flag=2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('c') flag=3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; when ('d') flag=4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; otherwise; /* this what you would do when none of the other values is encountered*/&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Select has a big advantage if the the number of values gets moderately large in that is usually easier to identify the condition&amp;nbsp;and the result quicker than if/then/else. Also the part after the condition can be a do/end code block and execute multiple statements which gets much messier with if/then/else.&lt;/P&gt;
&lt;P&gt;You can also provide lists of values in the when such as when ('a','f','g').&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 17:32:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/283840#M57874</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-13T17:32:19Z</dc:date>
    </item>
    <item>
      <title>Re: flagging cases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/284034#M57937</link>
      <description>&lt;P&gt;Thank you guys for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 14:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/flagging-cases/m-p/284034#M57937</guid>
      <dc:creator>Yogesh2601</dc:creator>
      <dc:date>2016-07-13T14:07:48Z</dc:date>
    </item>
  </channel>
</rss>

