<?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 How to efficient IF-ELSE in sas in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858655#M339255</link>
    <description>&lt;P&gt;Hi SAS Master,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to ask, how to simplify this IF-ELSE statement on SAS. Below is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
if 	year = 0 then bucket = 'M0 ';
else if year = 1 then bucket = 'M1 ';
else if year = 2 then bucket = 'M2 ';
else if year = 3 then bucket = 'M3 ';
else if year = 4 then bucket = 'M4 ';
else if year = 5 then bucket = 'M5 ';
else if year = 6 then bucket = 'M6 ';
else bucket = 'WO' ;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;feel free to format the &lt;STRONG&gt;&lt;EM&gt;bucket&lt;/EM&gt;&amp;nbsp;&lt;/STRONG&gt;whether this char or numeric.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Feb 2023 04:11:33 GMT</pubDate>
    <dc:creator>curious_guy</dc:creator>
    <dc:date>2023-02-14T04:11:33Z</dc:date>
    <item>
      <title>How to efficient IF-ELSE in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858655#M339255</link>
      <description>&lt;P&gt;Hi SAS Master,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to ask, how to simplify this IF-ELSE statement on SAS. Below is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
if 	year = 0 then bucket = 'M0 ';
else if year = 1 then bucket = 'M1 ';
else if year = 2 then bucket = 'M2 ';
else if year = 3 then bucket = 'M3 ';
else if year = 4 then bucket = 'M4 ';
else if year = 5 then bucket = 'M5 ';
else if year = 6 then bucket = 'M6 ';
else bucket = 'WO' ;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;feel free to format the &lt;STRONG&gt;&lt;EM&gt;bucket&lt;/EM&gt;&amp;nbsp;&lt;/STRONG&gt;whether this char or numeric.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 04:11:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858655#M339255</guid>
      <dc:creator>curious_guy</dc:creator>
      <dc:date>2023-02-14T04:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to efficient IF-ELSE in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858660#M339258</link>
      <description>&lt;P&gt;Actually no if/then/else is needed depending on how you intend to use those W0, W1 etc values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For many purposes you could create a custom format:&lt;/P&gt;
&lt;PRE&gt;proc format;
  value myear
0='M0'
1='M1'
2='M2'
3='M3'
4='M4'
5='M5'
6='M6'
other = 'WO'
;
run;&lt;/PRE&gt;
&lt;P&gt;Then assign the format Myear to the Year variable. Such as&lt;/P&gt;
&lt;PRE&gt;Proc print data=have;
   var year;
   format year myear. ;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The groups created by formats are honored by reporting and analysis procedures and graphing for many simple formats like this.&lt;/P&gt;
&lt;P&gt;The only complexity involved is making sure the format is available when needed, such as running the proc format code in each session before use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you really want a data step solution:&lt;/P&gt;
&lt;PRE&gt;if year in (0:6) then bucket=cats('M',year);
else bucket='WO';&lt;/PRE&gt;
&lt;P&gt;The IN checks to see if year is one of the listed integer values from 0 to 6 and if so uses the CATS function to append the year value to the letter M.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 05:33:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858660#M339258</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-14T05:33:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to efficient IF-ELSE in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858663#M339261</link>
      <description>&lt;P&gt;You could use select/when or use the format show by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; in a data step. If you need to the re-coding in multiple places, using a format is my preferred technique, because you have the definition in one place. Easier to read is the second way shown by ballardw, but whether such way exists depends largely on the re-coding itself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  
  bucket = put(year, myear.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2023 05:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858663#M339261</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-02-14T05:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to efficient IF-ELSE in sas</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858671#M339264</link>
      <description>&lt;P&gt;it works!!!!! thank youuu&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 07:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-efficient-IF-ELSE-in-sas/m-p/858671#M339264</guid>
      <dc:creator>curious_guy</dc:creator>
      <dc:date>2023-02-14T07:08:04Z</dc:date>
    </item>
  </channel>
</rss>

