<?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: generating new variable based on max cumulative sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806907#M318002</link>
    <description>Hi, thanks for the reply. I have started a new thread with very detailed steps. &lt;A href="https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-cumulative-sum/m-p/806906#M318001" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-cumulative-sum/m-p/806906#M318001&lt;/A&gt; Would you mind taking a look? Thank you very much!</description>
    <pubDate>Sat, 09 Apr 2022 04:14:50 GMT</pubDate>
    <dc:creator>stevenyan0127</dc:creator>
    <dc:date>2022-04-09T04:14:50Z</dc:date>
    <item>
      <title>generating new variable based on max cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806893#M317998</link>
      <description>&lt;P&gt;Hi, I have a dataset with 3 varibales, ID, Date, Cost in a long format. I want to generate a new variable called "Date_over_100" that shows the date when one's cumulative cost have exceeded 100.&amp;nbsp;&lt;SPAN&gt;I would appreciate all the help there is. Thanks in advance!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2022 00:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806893#M317998</guid>
      <dc:creator>stevenyan0127</dc:creator>
      <dc:date>2022-04-09T00:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: generating new variable based on max cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806895#M317999</link>
      <description>&lt;P&gt;Help us help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide a working DATA step with your sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And please provide a well-defined table showing what you expect the output dataset to look like.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2022 02:52:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806895#M317999</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-09T02:52:28Z</dc:date>
    </item>
    <item>
      <title>generating new variable based on cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806906#M318003</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, I have a dataset with 3 varibales, ID, Date, Cost in a long format. I want to generate a new variable called "Date_over_100" that shows the date when one's cumulative cost have exceeded 100.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;I would appreciate all the help there is. Thanks in advance! I have achieved this in R, but need help to do so in SAS.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;R:&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. I created the dataset first. I have repeated the generated 10 IDs so there are 100 in total, and generated 10 repetitions of 10 dates for each ID, and generated values for cost by random sampling 100 numbers between 30 and 50. And the output would look like the attachment file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;set.seed(11)&lt;BR /&gt;ID &amp;lt;- c(rep(1:10,each=10))&lt;BR /&gt;Date &amp;lt;- rep(sample(seq(as.Date('2008/01/01'), as.Date('2008/01/11'), by="day",replace=T), 10),10)&lt;BR /&gt;Cost &amp;lt;- round(runif(100,30,50),digits = 2)&lt;BR /&gt;df &amp;lt;- data.frame(ID,Date,Cost)&lt;BR /&gt;df &amp;lt;- df%&amp;gt;%&lt;BR /&gt;      arrange(Date)&lt;/PRE&gt;&lt;P&gt;2. I created a variable named "Date_Over_100" to show the date when one's cumulative cost has exceeded 100. Below are codes and output.&lt;/P&gt;&lt;PRE&gt;df_new &amp;lt;- df%&amp;gt;%
  group_by(ID) %&amp;gt;%
  summarize(Date_over_100 = Date[which.max(cumsum(Cost) &amp;gt; 100)])&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2022-04-09 at 12.06.10 AM.png" style="width: 224px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70270iECB16EE3DB526F8E/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2022-04-09 at 12.06.10 AM.png" alt="Screen Shot 2022-04-09 at 12.06.10 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3. I then transformed the original dataset into wide format.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;df_wide &amp;lt;- df%&amp;gt;%
  arrange(Date)%&amp;gt;%
  pivot_wider(names_from = Date, values_from = Cost)&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2022-04-09 at 12.07.03 AM.png" style="width: 789px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70271i9EE4091973A083CF/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2022-04-09 at 12.07.03 AM.png" alt="Screen Shot 2022-04-09 at 12.07.03 AM.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4. Lastly, I merged the wide format dataset with the dataset I got in 3.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;df_final &amp;lt;- inner_join(df_new,df_wide,by="ID")&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2022-04-09 at 12.07.38 AM.png" style="width: 902px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70272iD6B53A3C678A2684/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2022-04-09 at 12.07.38 AM.png" alt="Screen Shot 2022-04-09 at 12.07.38 AM.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Now in SAS:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1. Creating the dataset.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data a;
   call streaminit(123);
	do ID = 1 to 10;
	   do Date = '01Jan2008'd to '10Jan2008'd;
              Cost = rand("integer",30,50);
	      output;
	   end;
	end;
   format Date yymmddd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;First 15 rows of the output:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screen Shot 2022-04-08 at 11.54.37 PM.png" style="width: 939px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70269i972D2267D4F7416F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screen Shot 2022-04-08 at 11.54.37 PM.png" alt="Screen Shot 2022-04-08 at 11.54.37 PM.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;And next I need to create the variable "Date_over_100" that shows the date when one's cumulative cost has exceeded 100.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I have tried to replicate what I have done in R into SAS but haven't succeeded. I would appreciate any help there is. Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2022 04:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806906#M318003</guid>
      <dc:creator>stevenyan0127</dc:creator>
      <dc:date>2022-04-09T04:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: generating new variable based on max cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806907#M318002</link>
      <description>Hi, thanks for the reply. I have started a new thread with very detailed steps. &lt;A href="https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-cumulative-sum/m-p/806906#M318001" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-cumulative-sum/m-p/806906#M318001&lt;/A&gt; Would you mind taking a look? Thank you very much!</description>
      <pubDate>Sat, 09 Apr 2022 04:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806907#M318002</guid>
      <dc:creator>stevenyan0127</dc:creator>
      <dc:date>2022-04-09T04:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: generating new variable based on cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806908#M318004</link>
      <description>&lt;P&gt;See this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dara want;
set a;
by id;
retain flag;
if first.id
then do;
  sum_cost = cost;
  flag = 1;
end;
else sum_cost + cost; /* sum statement implies retain of sum_cost */
if sum_cost &amp;gt; 100
then do;
  if flag then output;
  flag = 0;
end;
drop flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS I merged your questions, no need for a new thread.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2022 06:26:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806908#M318004</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-09T06:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: generating new variable based on max cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806922#M318017</link>
      <description>&lt;P&gt;For each ID, I think you want one observation, with variable ID and DATE, representing the first date in which the cumulative sum of costs exceeds 100.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set a;
  by id;
  if first.id then sum_cost=0;
  sum_cost+cost;
  if (lag(sum_cost)&amp;lt;=100 or first.id=1) and sum_cost&amp;gt;100;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "subsetting if" outputs an observation only when the current sum_cost&amp;gt;100 and the observation-in-hand is either the first observation for a given ID, or is immediately preceded by an observation with sum_cost&amp;lt;=100.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Apr 2022 10:59:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806922#M318017</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-09T10:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: generating new variable based on cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806948#M318034</link>
      <description>Thank you Kurt! It works perfect!</description>
      <pubDate>Sat, 09 Apr 2022 21:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806948#M318034</guid>
      <dc:creator>stevenyan0127</dc:creator>
      <dc:date>2022-04-09T21:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: generating new variable based on max cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806949#M318035</link>
      <description>Thank you very much! It works perfect!</description>
      <pubDate>Sat, 09 Apr 2022 21:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generating-new-variable-based-on-max-cumulative-sum/m-p/806949#M318035</guid>
      <dc:creator>stevenyan0127</dc:creator>
      <dc:date>2022-04-09T21:42:43Z</dc:date>
    </item>
  </channel>
</rss>

