<?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: Check if a variable value by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577614#M163680</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211631"&gt;@newboy1218&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;The way you want your output data set shaped, you need a bit of logic to ensure that:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Before BALANCE becomes &amp;gt;500, EVER500=0&lt;/LI&gt;
&lt;LI&gt;Once BALANCE&amp;gt;500, EVER500=1 propagates though the end of the BY group&lt;/LI&gt;
&lt;LI&gt;OVER500 is incremented only on the records with BALANCE&amp;gt;500&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;For example (assuming that HAVE is sorted by customer):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                 
  input customer :$1. date balance ;        
  cards ;                                   
A  201804   200                             
A  201805   600                             
A  201806   450                             
A  201807  1000                             
B  201712     0                             
B  201801    50                             
B  201802   100                             
run ;                                       
                                            
data want ;                                 
  do until (last.customer) ;                
    set have ;                              
    by customer ;                           
    if not ever500 then ever500 = 0 ;       
    if balance &amp;gt; 500 then ever500 = 1 ;     
    over500 = sum (over500, balance &amp;gt; 500) ;
    output ;                                
  end ;                                     
run ;                                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It helps that the DATA step auto-resets EVER500 and OVER500 to missing values at the top of the implied loop, i.e. before the DO loop starts iterating.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jul 2019 06:15:15 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-07-30T06:15:15Z</dc:date>
    <item>
      <title>Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577545#M163655</link>
      <description>&lt;P&gt;Hi.. I have a dataset that organized by customer level and has a variable called 'balance'.&amp;nbsp; I want to check, for each customer, if their balance has ever gone above 500; if it has, how many times.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought about using array, but not sure how I can use it in this case. Does anyone have any suggestion?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;customer&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;balance&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;201804&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;201805&lt;/TD&gt;&lt;TD&gt;600&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;201806&lt;/TD&gt;&lt;TD&gt;450&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;201807&lt;/TD&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;201712&lt;/TD&gt;&lt;TD&gt;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gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;201801&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;201802&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 22:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577545#M163655</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-29T22:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577549#M163656</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by customer;
retain over500;
if first.customer then over500 = 0;
ever500 = (balance &amp;gt; 500);
over500 + ever500;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jul 2019 21:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577549#M163656</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-29T21:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577554#M163657</link>
      <description>Thank you. So do I need to sort the dataset by customer and date first?</description>
      <pubDate>Mon, 29 Jul 2019 21:51:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577554#M163657</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-29T21:51:35Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577557#M163658</link>
      <description>&lt;P&gt;And how can I retain the value of ever500 too?&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jul 2019 22:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577557#M163658</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-29T22:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577605#M163674</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211631"&gt;@newboy1218&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you. So do I need to sort the dataset by customer and date first?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 05:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577605#M163674</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T05:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577607#M163676</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211631"&gt;@newboy1218&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;And how can I retain the value of ever500 too?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ups, missed that one.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input customer $ date $ balance;
datalines;
A 201804 200
A 201805 600
A 201806 450
A 201807 1000
B 201712 0
B 201801 50
B 201802 100
;

data want;
set have;
by customer;
retain ever500 over500;
if first.customer
then do;
  ever500 = 0;
  over500 = 0;
end;
if balance &amp;gt; 500 then ever500 = 1;
over500 + (balance &amp;gt; 500);
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result now looks exactly like what you want:&lt;/P&gt;
&lt;PRE&gt;customer     date     balance    ever500    over500

   A        201804       200        0          0   
   A        201805       600        1          1   
   A        201806       450        1          1   
   A        201807      1000        1          2   
   B        201712         0        0          0   
   B        201801        50        0          0   
   B        201802       100        0          0   
&lt;/PRE&gt;
&lt;P&gt;Note how I presented the example data in a data step with datalines, so that the dataset can be created with a simple copy/paste and run. Please do so yourself in the future.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 05:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577607#M163676</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-30T05:36:17Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577614#M163680</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/211631"&gt;@newboy1218&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;The way you want your output data set shaped, you need a bit of logic to ensure that:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Before BALANCE becomes &amp;gt;500, EVER500=0&lt;/LI&gt;
&lt;LI&gt;Once BALANCE&amp;gt;500, EVER500=1 propagates though the end of the BY group&lt;/LI&gt;
&lt;LI&gt;OVER500 is incremented only on the records with BALANCE&amp;gt;500&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;For example (assuming that HAVE is sorted by customer):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                 
  input customer :$1. date balance ;        
  cards ;                                   
A  201804   200                             
A  201805   600                             
A  201806   450                             
A  201807  1000                             
B  201712     0                             
B  201801    50                             
B  201802   100                             
run ;                                       
                                            
data want ;                                 
  do until (last.customer) ;                
    set have ;                              
    by customer ;                           
    if not ever500 then ever500 = 0 ;       
    if balance &amp;gt; 500 then ever500 = 1 ;     
    over500 = sum (over500, balance &amp;gt; 500) ;
    output ;                                
  end ;                                     
run ;                                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It helps that the DATA step auto-resets EVER500 and OVER500 to missing values at the top of the implied loop, i.e. before the DO loop starts iterating.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2019 06:15:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577614#M163680</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-30T06:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577714#M163734</link>
      <description>Amazing Thank you!</description>
      <pubDate>Tue, 30 Jul 2019 13:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577714#M163734</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-30T13:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a variable value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577719#M163738</link>
      <description>Thank you so much!</description>
      <pubDate>Tue, 30 Jul 2019 13:24:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-variable-value-by-group/m-p/577719#M163738</guid>
      <dc:creator>newboy1218</dc:creator>
      <dc:date>2019-07-30T13:24:52Z</dc:date>
    </item>
  </channel>
</rss>

