<?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: Lag Basics (gvkey?) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724739#M225026</link>
    <description>&lt;P&gt;hm okay... something is severely wrong somewhere in my code &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Mar 2021 01:39:20 GMT</pubDate>
    <dc:creator>krg1140</dc:creator>
    <dc:date>2021-03-09T01:39:20Z</dc:date>
    <item>
      <title>Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724713#M225014</link>
      <description>&lt;P&gt;Hello everyone, me again!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question that I need is:&lt;/P&gt;&lt;P&gt;Create a variable, PY_AUDIT_FEE, equal to the audit fees paid by the same company in the prior year.&lt;/P&gt;&lt;P&gt;What I need to do is lag from the prior year but I don't understand how to do that? I have something like this currently set up:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data mergedata3; set mergedata3;&lt;BR /&gt;py_audit_fees = audit_fees=lag(audit_fees);&lt;BR /&gt;if audit_fees ne lag(audit_fees) then audit_fees=".";&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but it's only returning a 0 or a 1 so I'm confused where I'm going wrong? Any advice would be great!&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 00:15:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724713#M225014</guid>
      <dc:creator>krg1140</dc:creator>
      <dc:date>2021-03-09T00:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724726#M225023</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mergedata3;
  set mergedata3;
  prev_audit_fees = lag(audit_fees);
  if lag(gvkey)^=gvkey then prev_audit_fees=.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that all the yearly data for a given GVKEY are consecutive.&amp;nbsp; So the only problem is when the lag function retrieves the end of the prior gvkey and applies the result to PREV_AUDIT_FEES at the beginning of the next gvkey.&amp;nbsp; That's when the IF test resets prev_audit_fees to missing&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 00:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724726#M225023</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-09T00:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724739#M225026</link>
      <description>&lt;P&gt;hm okay... something is severely wrong somewhere in my code &lt;span class="lia-unicode-emoji" title=":anguished_face:"&gt;😧&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 01:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724739#M225026</guid>
      <dc:creator>krg1140</dc:creator>
      <dc:date>2021-03-09T01:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724760#M225036</link>
      <description>&lt;P&gt;I would start by sorting the data by gvkey and year. Then use a data-step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sorted;
  by gvkey;
  py_audit_fee = lag(py_audit_fee);
  if first.gvkey then py_audit_fee = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code is, of course untested. For tested code, please provide data in usable form -&amp;nbsp; a data step using datalines. Also note that&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mergedata3; set mergedata3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is always a bad idea. If something goes wrong, you have to re-create "mergdata3".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 06:13:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724760#M225036</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-03-09T06:13:32Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724767#M225040</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Always paste the code using the appropriate icon.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;it's only returning a 0 or a 1 so I'm confused where I'm going wrong?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; py_audit_fees =&lt;FONT color="#FF0000"&gt; audit_fees=lag(audit_fees)&amp;nbsp;&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;The part in red is a test. So the result is always 0 or 1 depending on whether the equality is true or not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 07:07:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724767#M225040</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2021-03-09T07:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724929#M225093</link>
      <description>&lt;P&gt;Another concern with the LAG function is that it is a queue function and when executed as part of an IF statement the lag is from the last time the IF was true.&lt;/P&gt;
&lt;P&gt;Consider this code:&lt;/P&gt;
&lt;PRE&gt;data example;
   input obs x y;
datalines;
1 1 2
2 2 3
3 1 4
4 2 5
5 1 7
6 1 2
7 2 99
;

data junk;
   set example;
   if x=2 then do;
      z=lag(y);
      put "observation is " obs "Lag y is " z;
   end; 
run;&lt;/PRE&gt;
&lt;P&gt;You might "expect" z for the first x=1 to have the value of y from the first obs, or from obs=4 that the value of z would be 4. But we actually get:&lt;/P&gt;
&lt;PRE&gt;observation is 2 Lag y is .
observation is 4 Lag y is 3
observation is 7 Lag y is 5
&lt;/PRE&gt;
&lt;P&gt;If you actually want the value from the immediately previous record you need to execute the LAG or DIF functions before the IF.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW if your "fees" variables are numeric, which seems extremely likely, you would set them to missing with either:&lt;/P&gt;
&lt;P&gt;variable = .;&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;call missing (variable);&lt;/P&gt;
&lt;P&gt;When you have a numeric variable and use var = '.'; then you will have SAS attempt to convert a character value period to numeric and generate messages like"&lt;/P&gt;
&lt;PRE&gt;NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
&lt;/PRE&gt;
&lt;P&gt;which can sometimes be a serious issue indicating your variable is of an unexpected type or a more complex syntax problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 18:04:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724929#M225093</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-09T18:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Basics (gvkey?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724949#M225101</link>
      <description>&lt;P&gt;Yes, but using LAG in an IF statement is very handy when dealing with interleave series.&amp;nbsp; Consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  input store $1. date :date9. sales;
  format date date9.;
datalines;
A 31jan2020 11
B 31jan2020 111
C 31jan2020 1111
A 29feb2020 22
B 29feb2020 222
C 29feb2020 2222
A 31mar2020 33
B 31mar2020 333
C 31mar2020 3333
A 30apr2020 44
B 30apr2020 444
C 30apr2020 4444
A 31may2020 55
B 31may2020 555
C 31may2020 5555
run;
data want;
  set have;
  if store='A' then prior_sales=lag(sales); else
  if store='B' then prior_sales=lag(sales); else
  if store='C' then prior_sales=lag(sales);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Each mention of the LAG function generates its own FIFO queue.&amp;nbsp; So prior_sales for store A is the last sales for A - no intemixing of prior sales from other stores.&amp;nbsp; BTW, this works even if sales records are randomly missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is one case where testing for all three stores simultaneously, as in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if store in ('A','B','C') then prior_sales=lag(sales);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is NOT equivalent to three separate if tests:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if store='A' then prior_sales=lag(sales); else
  if store='B' then prior_sales=lag(sales); else
  if store='C' then prior_sales=lag(sales);&lt;/CODE&gt;&lt;/PRE&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>Tue, 09 Mar 2021 19:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Basics-gvkey/m-p/724949#M225101</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-09T19:00:07Z</dc:date>
    </item>
  </channel>
</rss>

