<?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: Need previous year values to be missing in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707072#M37895</link>
    <description>&lt;P&gt;Here is an example of the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC TRANSPOSE DATA=WORK.DATA1&lt;BR /&gt;OUT=WORK.WANT(RENAME=(COL1=MONTH_VALUE))&lt;BR /&gt;NAME=CATGEORY;&lt;BR /&gt;BY DATE MONTH YEAR;&lt;BR /&gt;VAR CATEGORY_1 CATEGORY_2 CATEGORY_3 CATEGORY_4 ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC TRANSPOSE DATA=WORK.DATA2&lt;BR /&gt;OUT=WORK.WANT(RENAME=(COL1=YTD))&lt;BR /&gt;NAME=CATEGORY;&lt;BR /&gt;BY DATE MONTH YEAR;&lt;BR /&gt;VAR CATEGORY_1 CATEGORY_2 CATEGORY_3 CATEGORY_4 ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.WANT;&lt;BR /&gt;MERGE WORK.DATA1 WORK.DATA2;&lt;BR /&gt;BY DATE MONTH YEAR;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/** Calculates Previous Year Values for Each Metric **/&lt;BR /&gt;PROC SORT DATA=WORK.WANT;&lt;/P&gt;&lt;P&gt;BY METRIC YEAR;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;DATA WORK.WANT;&lt;BR /&gt;SET WORK.WANT;&lt;BR /&gt;BY METRIC YEAR;&lt;/P&gt;&lt;P&gt;PREVIOUS_YTD=LAG12(YTD_VALUE);&lt;BR /&gt;RUN;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Dec 2020 20:28:21 GMT</pubDate>
    <dc:creator>kathleen_07</dc:creator>
    <dc:date>2020-12-18T20:28:21Z</dc:date>
    <item>
      <title>Need previous year values to be missing</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707044#M37893</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set that reports values yearly and monthly. The first year given in a category I need the previous values to be missing. Here is a screenshot below with my general code being:&amp;nbsp;PREVIOUS_YTD=LAG12(YTD);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kathleen_07_0-1608316818533.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52830i027CCFDDACD46DCC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kathleen_07_0-1608316818533.png" alt="kathleen_07_0-1608316818533.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's with:&amp;nbsp;IF FIRST.YEAR THEN PREVIOUS_YTD=. ;&lt;BR /&gt;ELSE PREVIOUS_YTD=LAG12(YTD);&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kathleen_07_1-1608316953961.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/52831iC0CD6EB8BF44A136/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kathleen_07_1-1608316953961.png" alt="kathleen_07_1-1608316953961.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;In this example, 2018 is the first year for this category, so the previous YTD values should be missing. I have no idea where 86 or 104 is coming from. Possibly from the other categories?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 18:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707044#M37893</guid>
      <dc:creator>kathleen_07</dc:creator>
      <dc:date>2020-12-18T18:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: Need previous year values to be missing</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707061#M37894</link>
      <description>&lt;P&gt;LAG and DIF functions maintain a queue history. When you use an "if &amp;lt;condition&amp;gt; then value=lag(somevariable). The Lag returns the value the LAST time the condition was true.&lt;/P&gt;
&lt;P&gt;The typical work around is &lt;/P&gt;
&lt;P&gt;Lagvalue = Lag(variable);&lt;/P&gt;
&lt;P&gt;if &amp;lt;condtion&amp;gt; then wantedvalue=lagvalue;&amp;nbsp; &amp;lt;the variable created above&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then Drop the variable lagvalue when not needed later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It really is a good idea to include a complete data step as a single line of code does not show use everything you are doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First is just that : the first record with a value.&amp;nbsp; So the Feb through Dec records for the same year are no longer First.&amp;nbsp; So you will need to provide additional code and probably involves First for the mythical category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And your "data" does not show anything resembling "category" so can't address that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are certainly ways but we need to know what the data really looks like. Pictures do not work. I can't write code to read a picture that would create data to test with. For moderately complicated output then a better example of what the output should look like is also needed.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 20:00:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707061#M37894</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-18T20:00:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need previous year values to be missing</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707072#M37895</link>
      <description>&lt;P&gt;Here is an example of the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC TRANSPOSE DATA=WORK.DATA1&lt;BR /&gt;OUT=WORK.WANT(RENAME=(COL1=MONTH_VALUE))&lt;BR /&gt;NAME=CATGEORY;&lt;BR /&gt;BY DATE MONTH YEAR;&lt;BR /&gt;VAR CATEGORY_1 CATEGORY_2 CATEGORY_3 CATEGORY_4 ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC TRANSPOSE DATA=WORK.DATA2&lt;BR /&gt;OUT=WORK.WANT(RENAME=(COL1=YTD))&lt;BR /&gt;NAME=CATEGORY;&lt;BR /&gt;BY DATE MONTH YEAR;&lt;BR /&gt;VAR CATEGORY_1 CATEGORY_2 CATEGORY_3 CATEGORY_4 ;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA WORK.WANT;&lt;BR /&gt;MERGE WORK.DATA1 WORK.DATA2;&lt;BR /&gt;BY DATE MONTH YEAR;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/** Calculates Previous Year Values for Each Metric **/&lt;BR /&gt;PROC SORT DATA=WORK.WANT;&lt;/P&gt;&lt;P&gt;BY METRIC YEAR;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;DATA WORK.WANT;&lt;BR /&gt;SET WORK.WANT;&lt;BR /&gt;BY METRIC YEAR;&lt;/P&gt;&lt;P&gt;PREVIOUS_YTD=LAG12(YTD_VALUE);&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 20:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707072#M37895</guid>
      <dc:creator>kathleen_07</dc:creator>
      <dc:date>2020-12-18T20:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Need previous year values to be missing</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707079#M37896</link>
      <description>&lt;P&gt;First a warning:&lt;/P&gt;
&lt;PRE&gt;DATA WORK.WANT;
SET WORK.WANT;&lt;/PRE&gt;
&lt;P&gt;Every time you use this sort of code you are completely replacing the data set want. So a minor logic error or typo can result in replacing values with unwanted ones. So the next run does not have the correct input.&lt;/P&gt;
&lt;P&gt;While learning, or testing new sorts of code it much better to use&lt;/P&gt;
&lt;PRE&gt;DATA WORK.newname;
SET WORK.WANT;&lt;/PRE&gt;
&lt;P&gt;Is "metric" supposed to be your "category"? If so then maybe&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA WORK.new;
   SET WORK.WANT;
   BY  METRIC YEAR;
   retain referenceyear;
   lv = LAG12(YTD_VALUE);
   if first.metric then referenceyear=year; 
   if year ne referenceyear then PREVIOUS_YTD= lv;

   drop referenceyear lv;
RUN;&lt;/PRE&gt;
&lt;P&gt;If category is a different variable then you may need to sort by category metric year and use first.category. But without &lt;STRONG&gt;data&lt;/STRONG&gt; hard to tell. Also sorts by Year that need month order are always suspect. Date values are much more consistent in behavior.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 20:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707079#M37896</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-12-18T20:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Need previous year values to be missing</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707111#M37897</link>
      <description>&lt;P&gt;Why do you run the first proc transpose, which reads DATA1 and writes out dataset WORK.WANT, only to immediately follow it with another proc transpose reading DATA2 and over-writing dataset WANT?&amp;nbsp; Then why run the 2nd transpose, when you follow it with a DATA step that merges DATA1 and DATA2 to once again overwrite WANT?&amp;nbsp; The first two proc's are completely superfluous.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you&amp;nbsp; are starting the ytd calculations from a data set sorted by metric, and date, with exactly 12 monthly records per year, then this program will set previous_ytd to&amp;nbsp; missing for the first 12 records of each METRIC.&amp;nbsp; I name the dataset as HAVE, from which you would produce WANT:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  by metric date;
  previous_ytd=ifn(metric=lag12(metric),lag12(ytd),.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the "BY METRIC DATE" could be deleted, since the program code doesn't take advantage of it.&amp;nbsp; &amp;nbsp;But it does tell SAS to stop if the data is out of order.&amp;nbsp; &amp;nbsp;BTW, I assume that variable DATE is a true sas date value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IFN&amp;nbsp; statement tests the current metric vs the lag12(metric).&amp;nbsp; If they match (i.e. you are in the 13th and subsequent records), IFN returns previous_ytd=lag12(ytd).&amp;nbsp; Otherwise it's set to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the advantage of the IFN statement is that it will ALWAYS execute the LAG12(ytd) function, even if the METRIC=lag12(METRIC) condition is false, and IFN ultimately returns the third argument (a missing value).&amp;nbsp; In other words, the queue underneath the misleadingly-named LAG function is ALWAYS updated, even when IFN doesn't use it.&amp;nbsp; This is a good way to address the problem described by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 19 Dec 2020 01:43:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Need-previous-year-values-to-be-missing/m-p/707111#M37897</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-12-19T01:43:05Z</dc:date>
    </item>
  </channel>
</rss>

