<?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 Populate Variables with Previous Values in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617796#M19132</link>
    <description>&lt;P&gt;I would like to get the previous values populated into newly created variables prev_actual and prev_predict. However, I want the values to reset to missing at the beginning of the by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my code:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	set sashelp.prdsale;
run;

proc sort data =  have;
	by country region division prodtype product year month;
run;

data want;
	set have;
	by country region division prodtype product year month;

	if first.month then do;
	prev_actual = lag(actual);
	prev_predict = lag(predict);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is a part of the result I get, the change in product should result in missing prev_actual &amp;amp; missing prev_predict but the values from the previous product is populated:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ACTUAL	PREDICT	COUNTRY	REGION	DIVISION	PRODTYPE	PRODUCT	QUARTER	YEAR	MONTH	prev_actual	prev_predict
$387.00	$348.00	CANADA	EAST	CONSUMER	FURNITURE	BED	3	1994	Aug	490	311
$688.00	$458.00	CANADA	EAST	CONSUMER	FURNITURE	BED	3	1994	Sep	387	348
$650.00	$195.00	CANADA	EAST	CONSUMER	FURNITURE	BED	4	1994	Oct	688	458
$447.00	$658.00	CANADA	EAST	CONSUMER	FURNITURE	BED	4	1994	Nov	650	195
$91.00	$704.00	CANADA	EAST	CONSUMER	FURNITURE	BED	4	1994	Dec	447	658
&lt;STRONG&gt;&lt;FONT color="#993366"&gt;$5.00	$425.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	1	1993	Jan	91	704&lt;/FONT&gt;&lt;/STRONG&gt;
$164.00	$215.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	1	1993	Feb	5	425
$422.00	$948.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	1	1993	Mar	164	215
$424.00	$544.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	2	1993	Apr	422	948&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This row should be missing&amp;nbsp; but it is populated with prev_actual = 71 &amp;amp; prev_predict = 704&lt;/P&gt;
&lt;P&gt;&lt;FONT style="background-color: #ffffff;"&gt;ACTUAL&amp;nbsp;PREDICT&amp;nbsp;COUNTRY&amp;nbsp;REGION&amp;nbsp;DIVISION&amp;nbsp;PRODTYPE&amp;nbsp;PRODUCT&amp;nbsp;QUARTER&amp;nbsp;YEAR&amp;nbsp;MONTH&amp;nbsp;prev_actual&amp;nbsp;prev_predict&lt;BR /&gt;$5.00 &amp;nbsp; &amp;nbsp; &amp;nbsp; $425.00 &amp;nbsp; &amp;nbsp; CANADA &amp;nbsp;&amp;nbsp; EAST &amp;nbsp; CONSUMER&amp;nbsp;FURNITURE&amp;nbsp;SOFA&amp;nbsp;1&amp;nbsp;1993&amp;nbsp;Jan . .&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate some help in resolving this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2020 15:38:21 GMT</pubDate>
    <dc:creator>NewSASPerson</dc:creator>
    <dc:date>2020-01-16T15:38:21Z</dc:date>
    <item>
      <title>Populate Variables with Previous Values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617796#M19132</link>
      <description>&lt;P&gt;I would like to get the previous values populated into newly created variables prev_actual and prev_predict. However, I want the values to reset to missing at the beginning of the by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my code:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	set sashelp.prdsale;
run;

proc sort data =  have;
	by country region division prodtype product year month;
run;

data want;
	set have;
	by country region division prodtype product year month;

	if first.month then do;
	prev_actual = lag(actual);
	prev_predict = lag(predict);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is a part of the result I get, the change in product should result in missing prev_actual &amp;amp; missing prev_predict but the values from the previous product is populated:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ACTUAL	PREDICT	COUNTRY	REGION	DIVISION	PRODTYPE	PRODUCT	QUARTER	YEAR	MONTH	prev_actual	prev_predict
$387.00	$348.00	CANADA	EAST	CONSUMER	FURNITURE	BED	3	1994	Aug	490	311
$688.00	$458.00	CANADA	EAST	CONSUMER	FURNITURE	BED	3	1994	Sep	387	348
$650.00	$195.00	CANADA	EAST	CONSUMER	FURNITURE	BED	4	1994	Oct	688	458
$447.00	$658.00	CANADA	EAST	CONSUMER	FURNITURE	BED	4	1994	Nov	650	195
$91.00	$704.00	CANADA	EAST	CONSUMER	FURNITURE	BED	4	1994	Dec	447	658
&lt;STRONG&gt;&lt;FONT color="#993366"&gt;$5.00	$425.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	1	1993	Jan	91	704&lt;/FONT&gt;&lt;/STRONG&gt;
$164.00	$215.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	1	1993	Feb	5	425
$422.00	$948.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	1	1993	Mar	164	215
$424.00	$544.00	CANADA	EAST	CONSUMER	FURNITURE	SOFA	2	1993	Apr	422	948&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This row should be missing&amp;nbsp; but it is populated with prev_actual = 71 &amp;amp; prev_predict = 704&lt;/P&gt;
&lt;P&gt;&lt;FONT style="background-color: #ffffff;"&gt;ACTUAL&amp;nbsp;PREDICT&amp;nbsp;COUNTRY&amp;nbsp;REGION&amp;nbsp;DIVISION&amp;nbsp;PRODTYPE&amp;nbsp;PRODUCT&amp;nbsp;QUARTER&amp;nbsp;YEAR&amp;nbsp;MONTH&amp;nbsp;prev_actual&amp;nbsp;prev_predict&lt;BR /&gt;$5.00 &amp;nbsp; &amp;nbsp; &amp;nbsp; $425.00 &amp;nbsp; &amp;nbsp; CANADA &amp;nbsp;&amp;nbsp; EAST &amp;nbsp; CONSUMER&amp;nbsp;FURNITURE&amp;nbsp;SOFA&amp;nbsp;1&amp;nbsp;1993&amp;nbsp;Jan . .&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would appreciate some help in resolving this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 15:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617796#M19132</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2020-01-16T15:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: Populate Variables with Previous Values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617799#M19133</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/29035"&gt;@NewSASPerson&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to get the previous values populated into newly created variables prev_actual and prev_predict. However, I want the values to reset to missing at the beginning of the by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So does this mean you want a missing at the beginning of Country or the beginning of Region or the beginning of Division or ... etc. ... ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's assume it means each Product — this ought to have a missing value for the first record of each product.&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 country region division prodtype product year month;
	prev_actual = lag(actual);
	prev_predict = lag(predict);
	if first.product then do;
            prev_actual=.;
            prev_predict=.;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 15:38:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617799#M19133</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-16T15:38:43Z</dc:date>
    </item>
    <item>
      <title>Re: Populate Variables with Previous Values</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617802#M19134</link>
      <description>&lt;P&gt;Perfect! Thank you, That worked&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2020 15:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Populate-Variables-with-Previous-Values/m-p/617802#M19134</guid>
      <dc:creator>NewSASPerson</dc:creator>
      <dc:date>2020-01-16T15:39:52Z</dc:date>
    </item>
  </channel>
</rss>

