<?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 Creating buckets in Case When Statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401143#M97279</link>
    <description>&lt;P&gt;I'm trying to create a simple buckets in a Proc Sql and its not bucketing the groups correctly.&amp;nbsp;&amp;nbsp; No Errors, simply not adding the remainder in the 3rd bucket.&lt;/P&gt;&lt;P&gt;This appears to work correctly if I eliminate the 3rd case statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Select&lt;/P&gt;&lt;P&gt;firmware_ID, State, County, Zip, Miles_Avg,&lt;/P&gt;&lt;P&gt;Case when Miles_Avg &amp;lt;=20 then Miles_Avg 20 else bucket1,&lt;/P&gt;&lt;P&gt;Case when Miles_Avg&amp;nbsp;&amp;lt;=100 then (Miles_Avg&amp;nbsp;-bucket1) else 0 end as bucket2,&lt;/P&gt;&lt;P&gt;Case when Miles_Avg&amp;nbsp;&amp;lt;=250 then (Miles_Avg&amp;nbsp;-(bucket1+bucke2)) else 0 end as bucket3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from metrics2017&lt;/P&gt;&lt;P&gt;order by count;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 04 Oct 2017 19:54:17 GMT</pubDate>
    <dc:creator>tobyfarms</dc:creator>
    <dc:date>2017-10-04T19:54:17Z</dc:date>
    <item>
      <title>Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401143#M97279</link>
      <description>&lt;P&gt;I'm trying to create a simple buckets in a Proc Sql and its not bucketing the groups correctly.&amp;nbsp;&amp;nbsp; No Errors, simply not adding the remainder in the 3rd bucket.&lt;/P&gt;&lt;P&gt;This appears to work correctly if I eliminate the 3rd case statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Select&lt;/P&gt;&lt;P&gt;firmware_ID, State, County, Zip, Miles_Avg,&lt;/P&gt;&lt;P&gt;Case when Miles_Avg &amp;lt;=20 then Miles_Avg 20 else bucket1,&lt;/P&gt;&lt;P&gt;Case when Miles_Avg&amp;nbsp;&amp;lt;=100 then (Miles_Avg&amp;nbsp;-bucket1) else 0 end as bucket2,&lt;/P&gt;&lt;P&gt;Case when Miles_Avg&amp;nbsp;&amp;lt;=250 then (Miles_Avg&amp;nbsp;-(bucket1+bucke2)) else 0 end as bucket3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from metrics2017&lt;/P&gt;&lt;P&gt;order by count;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 19:54:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401143#M97279</guid>
      <dc:creator>tobyfarms</dc:creator>
      <dc:date>2017-10-04T19:54:17Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401145#M97280</link>
      <description>&lt;P&gt;Likely spelling:&lt;/P&gt;
&lt;P&gt;Case when Miles_Avg&amp;nbsp;&amp;lt;=250 then (Miles_Avg&amp;nbsp;-(bucket1+&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;bucke2&lt;/FONT&gt;&lt;/STRONG&gt;)) else 0 end as bucket3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Though for a large number of cases a simple format applied to the variable is more flexible than creating multiple variables.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 19:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401145#M97280</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-04T19:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401147#M97282</link>
      <description>&lt;P&gt;What does your log say? Why are there no END statements for the first CASE statement?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 19:59:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401147#M97282</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-04T19:59:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401167#M97300</link>
      <description>&lt;P&gt;If my Miles_Avg is 10, doesn't that equal the condition for all three case statements?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Oct 2017 21:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401167#M97300</guid>
      <dc:creator>HB</dc:creator>
      <dc:date>2017-10-04T21:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401181#M97305</link>
      <description>&lt;P&gt;I tried this&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Case when Miles_Avg &amp;lt;=20 then Miles_Avg 20 else bucket1,&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;it did not work for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Miles_Avg 20 - will this work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No end in your case too.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 01:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401181#M97305</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-10-05T01:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401185#M97307</link>
      <description>&lt;P&gt;What error messages do you get?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code would generate an error such as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ERROR: The following columns were not found in the contributing tables: bucket1&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;since the variable does not exist. You need this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Case when Miles_Avg&amp;nbsp;&amp;lt;=100 then (Miles_Avg&amp;nbsp;- &lt;STRONG&gt;calculated&lt;/STRONG&gt; bucket1) else 0 end as bucket2&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 04:00:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401185#M97307</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-10-05T04:00:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401295#M97353</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;, I never got an error message. The results would populate, just incorrectly.&amp;nbsp; Would I need to add the calculated function for the 'bucket3' string as well?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 11:51:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401295#M97353</guid>
      <dc:creator>tobyfarms</dc:creator>
      <dc:date>2017-10-05T11:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401400#M97381</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139205"&gt;@tobyfarms&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;, I never got an error message. The results would populate, just incorrectly.&amp;nbsp; Would I need to add the calculated function for the 'bucket3' string as well?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It always helps to provide a few rows of example input data and the output for that data.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 15:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401400#M97381</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-05T15:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating buckets in Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401405#M97383</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
		Select
			firmware_ID, State, County, Zip, Miles_Avg,
		Case 
			when Miles_Avg &amp;lt;=20 then Miles_Avg 
			else 0 
		end as bucket1,
		Case 
			when Miles_Avg &amp;lt;=100 then (Miles_Avg - CALCULATED bucket1) 
			else 0 
		end as bucket2,
		Case 
			when Miles_Avg &amp;lt;=250 then (Miles_Avg -(CALCULATED bucket1 + CALCULATED bucket2)) 
			else 0 
		end as bucket3
		from metrics2017
			order by count;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think this may be what you're looking for... or at least closer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Use the CALCULATED keyword before a variable you're using that's created in the same query&lt;/P&gt;
&lt;P&gt;2. All CASES should have an END AS &amp;lt;variable name&amp;gt;&lt;/P&gt;
&lt;P&gt;3. You have some syntax errors (miles_avg 20) -&amp;gt; 20 is what? extra space there?&lt;/P&gt;
&lt;P&gt;3b. bucke2 should be bucket2 most likely&lt;/P&gt;
&lt;P&gt;4. You likely have some logic issues, but since you didn't state your logic and your code is wrong I'm not sure we can make suggestions.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please review the guidelines here on how to ask a question. This is a simple question and you could have had an answer significantly earlier if you follow the guidelines:&amp;nbsp;&lt;A href="https://stackoverflow.com/help/how-to-ask" target="_blank"&gt;https://stackoverflow.com/help/how-to-ask&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2017 15:48:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-buckets-in-Case-When-Statement/m-p/401405#M97383</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-05T15:48:27Z</dc:date>
    </item>
  </channel>
</rss>

