<?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: SAS if then else and banding numbers into groups in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590318#M168929</link>
    <description>Thanks for your reply - it's just if BAND is equal to ANY ONE of the five options. So if an observation has a GROUP value of 1, they would have 'GROUP=1' for the newly created BAND variable, which in turn means that BAND_1_to_5 would have a result of 'Yes' for that observation</description>
    <pubDate>Fri, 20 Sep 2019 10:57:02 GMT</pubDate>
    <dc:creator>jeremy4</dc:creator>
    <dc:date>2019-09-20T10:57:02Z</dc:date>
    <item>
      <title>SAS if then else and banding numbers into groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590303#M168920</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to band the variable 'GROUP' in my want dataset, so that it:&lt;/P&gt;&lt;P&gt;1. Creates a new variable 'BAND' in the 'test' dataset&lt;/P&gt;&lt;P&gt;2. Create a new BAND_1_to_5 variable, which will use the newly created 'BAND' variable just created.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BAND_1_to_5 will be equal to 'Yes' when the new 'BAND' variable created has:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BAND='GROUP = 1'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BAND='GROUP = 2'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BAND='GROUP= 3'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BAND='GROUP = 4'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BAND='GROUP = 5'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;else BAND_1_to_5 will be equal to 'No' for any other result e.g.&amp;nbsp;Band = 'GROUP = -99999',&amp;nbsp;Band = 'GROUP = -99998' etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please can the code be reviewed below to check if the new&amp;nbsp;BAND_1_to_5 variable I want to create will produce 'Yes' and 'No' results required, and &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;whether BAND &lt;U&gt;and&lt;/U&gt; BAND_1_to_5 &lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT color="#000000"&gt;can both be created in one DATA step (see question 2 below)&lt;/FONT&gt;, or whether BAND_1_to_5 needs to be created in a separate DATA step (see the two questions below)? Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Current code&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;length Band $25;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if GROUP = -99999 then Band = 'GROUP = -99999';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if&amp;nbsp;GROUP = -99998 then Band = 'GROUP = -99998';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if -99997 &amp;lt;= GROUP &amp;lt;=-1 then Band = '-99997 &amp;lt;= GROUP &amp;lt;=-1';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 0 then Band = 'GROUP = 0';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else&amp;nbsp; if GROUP = 1 then Band ='GROUP = 1';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 2 then Band = 'GROUP = 2';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 3 then Band = 'GROUP = 3';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 4 then Band = 'GROUP = 4';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 5 then Band = 'GROUP = 5';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if 6 &amp;lt;= GROUP &amp;lt;= 10 then Band = 'GROUP = 6-10';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else Band = 'GROUP = 11+';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1. Would the code below be correct (using &lt;FONT color="#FF0000"&gt;in ([all of the GROUP results from 1-5])&lt;/FONT&gt; so that BAND_1_to_5 has a result of 'Yes' when GROUP=1, GROUP=2, GROUP=3, GROUP=4 and GROUP=5 only (see code below)?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set test;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;length Band_1_to_5 $3;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if &lt;FONT color="#FF0000"&gt;BAND in ('GROUP = 1', 'GROUP = 2', 'GROUP= 3', 'GROUP = 4', 'GROUP = 5') then BAND_1_to_5 = 'Yes';&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;else BAND_1_to_5 = 'No';&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2. OR can I put the code&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; (if BAND in ('GROUP = 1', 'GROUP = 2', 'GROUP= 3', 'GROUP = 4', 'GROUP = 5') then BAND_1_to_5 = 'Yes';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;else BAND_1_to_5 = 'No'; )&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp;in the code written above, so that both that both new variables, BAND and BAND_1_to_5 are BOTH included in the just the one 'test' dataset (i.e. everything is created from just one DATA step).&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;For example:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data test;&lt;BR /&gt;set want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;length Band $25;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if GROUP = -99999 then Band = 'GROUP = -99999';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if&amp;nbsp;GROUP = -99998 then Band = 'GROUP = -99998';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if -99997 &amp;lt;= GROUP &amp;lt;=-1 then Band = '-99997 &amp;lt;= GROUP &amp;lt;=-1';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 0 then Band = 'GROUP = 0';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else&amp;nbsp; if GROUP = 1 then Band ='GROUP = 1';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 2 then Band = 'GROUP = 2';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 3 then Band = 'GROUP = 3';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 4 then Band = 'GROUP = 4';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if GROUP = 5 then Band = 'GROUP = 5';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else if 6 &amp;lt;= GROUP &amp;lt;= 10 then Band = 'GROUP = 6-10';&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else Band = 'GROUP = 11+';&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;FONT color="#FF0000"&gt;if BAND in ('GROUP = 1', 'GROUP = 2', 'GROUP= 3', 'GROUP = 4', 'GROUP = 5') then BAND_1_to_5 = 'Yes';&amp;nbsp; &amp;nbsp;/*Is it possible to create BAND_1_to_5 in the same DATA step, when GROUP is a new variable being created? */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;else BAND_1_to_5 = 'No';&lt;/FONT&gt;&lt;BR /&gt;run;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 09:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590303#M168920</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-09-20T09:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: SAS if then else and banding numbers into groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590314#M168926</link>
      <description>&lt;PRE&gt;1. Would the code below be correct (using in ([all of the GROUP results from 1-5]) so that BAND_1_to_5 has a result of 'Yes' when GROUP=1, GROUP=2, GROUP=3, GROUP=4 and GROUP=5 only (see code below)?

 

data test2;
   set test;
   length Band_1_to_5 $3;
   if BAND in ('GROUP = 1', 'GROUP = 2', 'GROUP= 3', 'GROUP = 4', 'GROUP = 5') then BAND_1_to_5 = 'Yes';
     else BAND_1_to_5 = 'No';

run;

 

2. OR can I put the code 

    (if BAND in ('GROUP = 1', 'GROUP = 2', 'GROUP= 3', 'GROUP = 4', 'GROUP = 5') then BAND_1_to_5 = 'Yes';
     else BAND_1_to_5 = 'No'; )

   in the code written above, so that both that both new variables, BAND and BAND_1_to_5 are BOTH included in the just the one 'test' dataset (i.e. everything is created from just one DATA step).&lt;/PRE&gt;
&lt;P&gt;[1] Do you want the combination in 1. to be all 5 to simultaneously true to take value 'YES'? Or if one of them (eg. 'GROUP = 4' ) is true then do you want 'YES'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A better way will be to add few records in DATALINES and show an output you require out of it.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 10:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590314#M168926</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-09-20T10:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS if then else and banding numbers into groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590315#M168927</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why don't you just try it and see if it works ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the newly created variable BAND in order to create the new variable BAND_1_to_5 in the same&lt;/P&gt;
&lt;P&gt;data step but it is simpler to use the variable GROUP as it is numeric so you don't have potential problems with case&lt;/P&gt;
&lt;P&gt;or blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input GROUP;
    cards;
-99999
-99998
-99997
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
;
run;

data test;
    set have;
    length Band $25;

    if GROUP in (-99999,-99998) or (0&amp;lt;=GROUP&amp;lt;=5) then Band = catx(' ','GROUP =', GROUP);
    else if -99997 &amp;lt;= GROUP &amp;lt;=-1 then Band = '-99997 &amp;lt;= GROUP &amp;lt;=-1';
    else if 6 &amp;lt;= GROUP &amp;lt;= 10 then Band = 'GROUP = 6-10';
    else Band = 'GROUP = 11+';

    if (1&amp;lt;=GROUP&amp;lt;=5) then BAND_1_to_5 = 'Yes';
    else BAND_1_to_5 = 'No';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 10:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590315#M168927</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-09-20T10:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS if then else and banding numbers into groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590318#M168929</link>
      <description>Thanks for your reply - it's just if BAND is equal to ANY ONE of the five options. So if an observation has a GROUP value of 1, they would have 'GROUP=1' for the newly created BAND variable, which in turn means that BAND_1_to_5 would have a result of 'Yes' for that observation</description>
      <pubDate>Fri, 20 Sep 2019 10:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590318#M168929</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-09-20T10:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS if then else and banding numbers into groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590320#M168930</link>
      <description>Thanks a lot, that's definitely a better way!</description>
      <pubDate>Fri, 20 Sep 2019 11:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590320#M168930</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-09-20T11:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS if then else and banding numbers into groups</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590439#M168983</link>
      <description>&lt;P&gt;Creating custom formats for variables removes the need to keep adding variables.&lt;/P&gt;
&lt;P&gt;Groups created by formats are honored by most analysis, reporting and graphic procedures. So you can use a different format with the same variable to create different groups for an analysis or report.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Sep 2019 15:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-if-then-else-and-banding-numbers-into-groups/m-p/590439#M168983</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-20T15:17:26Z</dc:date>
    </item>
  </channel>
</rss>

