<?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: Using select statement in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244038#M55996</link>
    <description>&lt;P&gt;As others have said: respect the syntax. If it is not described in the syntax definition, it won't work.&lt;/P&gt;
&lt;P&gt;Also, the conditions in s select block are tested sequentially until one of them is true; other conditions after that one are not tested. So you must test the highest range first because if Expense is greater than 50000 then it is also greater than 4000. If you test it against 4000 first, you will never test it against 50000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp_sw;
set ramu.temp;
length spendanalysed $25;
select;
	when (Expense&amp;gt;50000) spendanalysed="Affluent";
	when (Expense&amp;gt;20000) spendanalysed="Middle class";
	when (Expense&amp;gt;4000) spendanalysed="Low class";
	otherwise spendanalysed="Poor";
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 17 Jan 2016 03:04:04 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-01-17T03:04:04Z</dc:date>
    <item>
      <title>Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/243998#M55990</link>
      <description>&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Code as written by me: Approach1&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;data temp_sw;&lt;/P&gt;&lt;P&gt;set ramu.temp;&lt;/P&gt;&lt;P&gt;select;&lt;/P&gt;&lt;P&gt;when (Expense&amp;gt;4000) spendanalysed="Low class";&lt;/P&gt;&lt;P&gt;and when (Expense&amp;gt;20000) spendanalysed="Middle class";&lt;/P&gt;&lt;P&gt;and when (Expense&amp;gt;50000) spendanalysed="Affluent";&lt;/P&gt;&lt;P&gt;otherwise spendanalysed="Poor";&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;Code as written by me: Approach2&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data temp_sw;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set ramu.temp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;length spendanalysed $25;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;select;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if (Expense&amp;gt;4000) spendanalysed="Low class";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;else if (Expense&amp;gt;20000) spendanalysed="Middle class";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;else if (Expense&amp;gt;50000) spendanalysed="Affluent";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;else do spendanalysed="Poor";&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;In both aprroaches, i am getting an error in the log. the respective logs are attached below for your reference.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Please help:-(&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Jan 2016 15:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/243998#M55990</guid>
      <dc:creator>bgs</dc:creator>
      <dc:date>2016-01-16T15:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/243999#M55991</link>
      <description>&lt;P&gt;Attachements of Logs&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12199iEFB2F065C24B0E87/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Approach1 - log.PNG" title="Approach1 - log.PNG" /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12200i6E11667F2604B248/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Approach2 - log.PNG" title="Approach2 - log.PNG" /&gt;</description>
      <pubDate>Sat, 16 Jan 2016 15:42:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/243999#M55991</guid>
      <dc:creator>bgs</dc:creator>
      <dc:date>2016-01-16T15:42:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244009#M55992</link>
      <description>&lt;P&gt;Simply stick to the syntax of the SELECT statement and do not write ANDs before WHEN, let alone replace WHEN by IF/ELSE IF.&lt;/P&gt;</description>
      <pubDate>Sat, 16 Jan 2016 17:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244009#M55992</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-16T17:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244026#M55994</link>
      <description>If you did want to use IF/ELSE remember the THEN. Its always helpful to check against the doc as well. &lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202239.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000202239.htm&lt;/A&gt;</description>
      <pubDate>Sun, 17 Jan 2016 01:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244026#M55994</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-17T01:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244038#M55996</link>
      <description>&lt;P&gt;As others have said: respect the syntax. If it is not described in the syntax definition, it won't work.&lt;/P&gt;
&lt;P&gt;Also, the conditions in s select block are tested sequentially until one of them is true; other conditions after that one are not tested. So you must test the highest range first because if Expense is greater than 50000 then it is also greater than 4000. If you test it against 4000 first, you will never test it against 50000.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp_sw;
set ramu.temp;
length spendanalysed $25;
select;
	when (Expense&amp;gt;50000) spendanalysed="Affluent";
	when (Expense&amp;gt;20000) spendanalysed="Middle class";
	when (Expense&amp;gt;4000) spendanalysed="Low class";
	otherwise spendanalysed="Poor";
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jan 2016 03:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244038#M55996</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-01-17T03:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244054#M55998</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt;&amp;nbsp;- Thank you. I can understand this now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another way to write the code for the same outcome is I believe:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data temp_sw;&lt;BR /&gt;set ramu.temp;&lt;BR /&gt;length spendanalysed $25;&lt;BR /&gt;select(Expense); &lt;STRONG&gt;/*here you can select the variable and then define the conditions*/&lt;/STRONG&gt;&lt;BR /&gt;when(&amp;gt;50000) spendanalysed="Affluent";&lt;BR /&gt;when(&amp;gt;20000) spendanalysed="Middle class";&lt;BR /&gt;when(&amp;gt;4000) spendanalysed="Low class";&lt;BR /&gt;otherwise spendanalysed="Poor";&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you also correct the syntax here - I am getting an error in the log&lt;/P&gt;&lt;P&gt;Or is it that I cannot use this form of entry for tjhe desired output?&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jan 2016 10:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244054#M55998</guid>
      <dc:creator>bgs</dc:creator>
      <dc:date>2016-01-17T10:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using select statement</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244060#M56001</link>
      <description>&lt;P&gt;Please have a look at&amp;nbsp;the &lt;A href="http://support.sas.com/documentation/cdl/en/syntaxidx/68719/HTML/default/index.htm#/documentation/cdl//en/lestmtsref/68024/HTML/default/p09213s9jc2t99n1vx0omk2rh9ps.htm" target="_blank"&gt;documentation of the SELECT statement&lt;/A&gt;.&amp;nbsp;There, in the short paragraph titled "Evaluating the when-expression When a select-expression Is Included" (which applies to the situation that you write &lt;FONT face="courier new,courier"&gt;select(&lt;EM&gt;something&lt;/EM&gt;);&lt;/FONT&gt; rather than &lt;FONT face="courier new,courier"&gt;select;&lt;/FONT&gt;), it says: "&lt;SPAN&gt;SAS evaluates the &lt;/SPAN&gt;&lt;SPAN class="xis-userSuppliedValue"&gt;select-expression&lt;/SPAN&gt;&lt;SPAN&gt; and &lt;/SPAN&gt;&lt;SPAN class="xis-userSuppliedValue"&gt;when-expression&lt;/SPAN&gt;&lt;SPAN&gt;. SAS compares the two for equality and returns a value of true or false."&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your when-expressions (e.g. "&amp;gt;50000"), however, cannot be evaluated (to a number or character string). They are not even valid SAS expressions, because they are incomplete. In particular, they cannot be equal to any of the numeric values of Expense (the select-expression).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, if Expense was your select-expression, you would have to list all possible values (at least those present in the data) in the respective categories of the WHEN expressions, e.g. &lt;FONT face="courier new,courier"&gt;WHEN(4000.01, 4000.02, 4000.03,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;lt;many pages later and assuming that your Expense values don't have more than two decimals&amp;gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;19999.97, 19999.98, 19999.99, 20000) spendanalysed="Low class";&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;You see: This is infeasible with your intended WHEN conditions. Hence, stick to PG's suggestion.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;One more hint: Please note that missing values of Expense would be classified as "Poor" in variable spendanalysed, because they don't satisfy any of the three WHEN conditions. To avoid this, you could add a fourth WHEN condition and statement, e.g.&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;when (missing(Expense)) spendanalysed="Missing";&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jan 2016 15:07:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-select-statement/m-p/244060#M56001</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-17T15:07:23Z</dc:date>
    </item>
  </channel>
</rss>

