<?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 By Statement with a variable that not exists in the input dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769133#M243993</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm facing a problem that I can't resolve in one step.&lt;/P&gt;&lt;P&gt;So I would like to output the sum of volume for each year with using by statement. However I can't do that because the variable year doesn't exist in the input set.&lt;/P&gt;&lt;P&gt;I know I can do it in 2 step but I don't want to because with the real database I'm using it will be too heavy.&amp;nbsp;&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;
format date ddmmyy8.;
Date="01mar2015"d;
duration=24;
Volume=10;
run;

Data want;
set have;
by year;
retain Volume_cumul 0;
Volume_cumul=Volume;

do i=0 to Duration;
Year=year(intnx('month',date,i,'s'));
Month=month(intnx('month',date,i,'s'));
Volume_cumul=Volume_cumul+1;
if last.year=1 then output;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thx for your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Sep 2021 12:40:27 GMT</pubDate>
    <dc:creator>adil256</dc:creator>
    <dc:date>2021-09-22T12:40:27Z</dc:date>
    <item>
      <title>By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769133#M243993</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm facing a problem that I can't resolve in one step.&lt;/P&gt;&lt;P&gt;So I would like to output the sum of volume for each year with using by statement. However I can't do that because the variable year doesn't exist in the input set.&lt;/P&gt;&lt;P&gt;I know I can do it in 2 step but I don't want to because with the real database I'm using it will be too heavy.&amp;nbsp;&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;
format date ddmmyy8.;
Date="01mar2015"d;
duration=24;
Volume=10;
run;

Data want;
set have;
by year;
retain Volume_cumul 0;
Volume_cumul=Volume;

do i=0 to Duration;
Year=year(intnx('month',date,i,'s'));
Month=month(intnx('month',date,i,'s'));
Volume_cumul=Volume_cumul+1;
if last.year=1 then output;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thx for your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 12:40:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769133#M243993</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2021-09-22T12:40:27Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769134#M243994</link>
      <description>&lt;P&gt;Don't write your own DATA step code to compute sums over groups. SAS has already done this work for you and it is called either PROC MEANS or PROC SUMMARY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have nway;
    class date;
    format date year4.;
    var volume;
    output out=_sums_ sum=;
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>Wed, 22 Sep 2021 12:44:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769134#M243994</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-22T12:44:39Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769139#M243996</link>
      <description>&lt;P&gt;Hi thank u for u answer but your solution doesn't meet my needs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Actually I'm performing a lot of calculation and I need to get the cumulative sum at the end of year this way.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 12:52:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769139#M243996</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2021-09-22T12:52:13Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769140#M243997</link>
      <description>&lt;P&gt;"cumulative sum at the end of the year" and sum from the PROC SUMMARY code are identical and equal and have no difference. Or you haven't explained what you want clearly enough.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 12:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769140#M243997</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-22T12:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769144#M243999</link>
      <description>&lt;P&gt;Can you please post what you ultimately want? I run your code but can't really tell what we're trying to get at.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 12:56:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769144#M243999</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-09-22T12:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769152#M244004</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;If u run the code I posted earlier , u'll have an error message saying that the By variable Year does not exist in the input data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to get the dataset want2 in one step.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
format date ddmmyy8.;
Date="01mar2015"d;
duration=24;
Volume=10;
run;

Data want;
set have;
*by year;
retain Volume_cumul 0;
Volume_cumul=Volume;

do i=0 to Duration;
Year=year(intnx('month',date,i,'s'));
Month=month(intnx('month',date,i,'s'));
Volume_cumul=Volume_cumul+1;
output;*if last.year=1 then output;
end;

run;
proc sort data=want;
by year;
run;
Data want2;
set want;
by year;
if last.year=1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Sep 2021 13:09:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769152#M244004</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2021-09-22T13:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769154#M244005</link>
      <description>&lt;P&gt;Yes, I understand that, and I did see that error when I ran it. It makes sense that it didn't run.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I am asking for, however, is your final data set -- what will it look like?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Providing that ensures that we are able to get exactly what you want.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 13:12:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769154#M244005</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-09-22T13:12:22Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769155#M244006</link>
      <description>&lt;P&gt;Yeah technically it does the same job but I'll need to output the entire database in first step before taking the sum which I don't.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Finally I just want to use the statement last.variable with a variable that doesn't exist in the input dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 13:15:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769155#M244006</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2021-09-22T13:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769157#M244007</link>
      <description>&lt;P&gt;I'd like to get the dataset Want2&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
format date ddmmyy8.;
Date="01mar2015"d;
duration=24;
Volume=10;
run;

Data want;
set have;
*by year;
retain Volume_cumul 0;
Volume_cumul=Volume;

do i=0 to Duration;
Year=year(intnx('month',date,i,'s'));
Month=month(intnx('month',date,i,'s'));
Volume_cumul=Volume_cumul+1;
output;*if last.year=1 then output;
end;

run;
proc sort data=want;
by year;
run;

Data want2;
set want;
by year;
if last.year=1 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Sep 2021 13:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769157#M244007</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2021-09-22T13:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769164#M244014</link>
      <description>&lt;P&gt;The simple answer is NO.&amp;nbsp; You can simulate the FIRST. flag by using LAG.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  x=&amp;lt;some function&amp;gt;;
  first_x  = x ne lag(x);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But to calculate LAST. flag you have to know what the next value of X, that you have not calculated yet, is going to be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not generate the derived dataset as a view and then use the BY statement when processing the VIEW?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if your real use case is as simple as your example why not just test if MONTH=12?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=0 to Duration;
  Year=year(intnx('month',date,i,'s'));
  Month=month(intnx('month',date,i,'s'));
  Volume_cumul=Volume_cumul+1;
  if Month=12 or i=duration then output;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Sep 2021 13:32:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769164#M244014</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-22T13:32:57Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769170#M244017</link>
      <description>&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&amp;nbsp;I absolutely wanted to use the last.variable statement to output the data that&amp;nbsp; I didn't think at all at this solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thx for ur help&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 13:57:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769170#M244017</guid>
      <dc:creator>adil256</dc:creator>
      <dc:date>2021-09-22T13:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: By Statement with a variable that not exists in the input dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769174#M244019</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/43088"&gt;@adil256&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yeah technically it does the same job but I'll need to output the entire database in first step before taking the sum which I don't.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I just don't understand how "output the entire database" relates to the rest of the problem you have described.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You were previously asked to SHOW US what you want (or a small portion of the output), and that hasn't been done either.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Sep 2021 14:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-Statement-with-a-variable-that-not-exists-in-the-input/m-p/769174#M244019</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-22T14:28:40Z</dc:date>
    </item>
  </channel>
</rss>

