<?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 How Do I Retain Values between a data step using an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354639#M82984</link>
    <description>&lt;P&gt;Good evening,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to figure out what I believe should be a relatively straight forward problem im SAS. Please see the attached SAS file which describes what I am trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, I have a data set that has a bunch of ID's and these ID's are repeated throughout the dataset for the specified time period (5 months in the example attached). So each of those ID's would have 5 records in this data set, for the 5 months we are covering with a value for a new monthly demand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first record of each ID in this dataset, we have the previous 3 months of demand. However, I want to use an array to shift that past demand over and feed in the new value that we have in the "new_demand" column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if my explanation makes sense. If anyone here could help me with this issue it would be greatly appreciated!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data existing;
   infile datalines delimiter=','; 
   input id $ monthid new_demand month1 month2 month3;
   datalines;                      
1,1,2,3,4,5
1,2,3,.,.,.
1,3,0,.,.,.
1,4,3,.,.,.
1,5,1,.,.,.
2,1,2,3,4,5
2,2,3,.,.,.
2,3,0,.,.,.
2,4,3,.,.,.
2,5,1,.,.,.
;
run;


data new;
	set existing;
	by id;
	retain month1-month3;
	array monthly[3] month1-month3;

	put "month1=" month1;
	put "month2=" month2;

	demand_lag = lag(new_demand);
	monthlag1 = lag(month1);
	monthlag2 = lag(month2);
	monthlag3 = lag(month3);

	array lag_test[3] monthlag1-monthlag3;

		do i=dim(monthly) to 1 by -1;
			if i=1 then do;
				monthly[i]=demand_lag;
			end;
			else do;
				monthly[i]=lag_test[i-1];
			end;
		end;

run;


data desired;
   infile datalines delimiter=','; 
   input id $ monthid new_demand month1 month2 month3;
   datalines;                      
1,1,2,3,4,5
1,2,3,2,3,4
1,3,0,3,2,3
1,4,3,0,3,2
1,5,1,3,0,3
2,1,2,3,4,5
2,2,3,2,3,4
2,3,0,3,2,3
2,4,3,0,3,2
2,5,1,3,0,3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 29 Apr 2017 03:26:33 GMT</pubDate>
    <dc:creator>sds2ga</dc:creator>
    <dc:date>2017-04-29T03:26:33Z</dc:date>
    <item>
      <title>How Do I Retain Values between a data step using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354639#M82984</link>
      <description>&lt;P&gt;Good evening,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to figure out what I believe should be a relatively straight forward problem im SAS. Please see the attached SAS file which describes what I am trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, I have a data set that has a bunch of ID's and these ID's are repeated throughout the dataset for the specified time period (5 months in the example attached). So each of those ID's would have 5 records in this data set, for the 5 months we are covering with a value for a new monthly demand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the first record of each ID in this dataset, we have the previous 3 months of demand. However, I want to use an array to shift that past demand over and feed in the new value that we have in the "new_demand" column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if my explanation makes sense. If anyone here could help me with this issue it would be greatly appreciated!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data existing;
   infile datalines delimiter=','; 
   input id $ monthid new_demand month1 month2 month3;
   datalines;                      
1,1,2,3,4,5
1,2,3,.,.,.
1,3,0,.,.,.
1,4,3,.,.,.
1,5,1,.,.,.
2,1,2,3,4,5
2,2,3,.,.,.
2,3,0,.,.,.
2,4,3,.,.,.
2,5,1,.,.,.
;
run;


data new;
	set existing;
	by id;
	retain month1-month3;
	array monthly[3] month1-month3;

	put "month1=" month1;
	put "month2=" month2;

	demand_lag = lag(new_demand);
	monthlag1 = lag(month1);
	monthlag2 = lag(month2);
	monthlag3 = lag(month3);

	array lag_test[3] monthlag1-monthlag3;

		do i=dim(monthly) to 1 by -1;
			if i=1 then do;
				monthly[i]=demand_lag;
			end;
			else do;
				monthly[i]=lag_test[i-1];
			end;
		end;

run;


data desired;
   infile datalines delimiter=','; 
   input id $ monthid new_demand month1 month2 month3;
   datalines;                      
1,1,2,3,4,5
1,2,3,2,3,4
1,3,0,3,2,3
1,4,3,0,3,2
1,5,1,3,0,3
2,1,2,3,4,5
2,2,3,2,3,4
2,3,0,3,2,3
2,4,3,0,3,2
2,5,1,3,0,3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Apr 2017 03:26:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354639#M82984</guid>
      <dc:creator>sds2ga</dc:creator>
      <dc:date>2017-04-29T03:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Retain Values between a data step using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354673#M82996</link>
      <description>&lt;P&gt;Use a temporary array&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ata desired;
array m_{99} _temporary_;
set existing; by id;
array month{*} month1-month3;
if first.id then do;
    call missing(of m_{*});
    do i = 1 to dim(month);
        m_{i+1} = month{i};
        end;
    end;
else do;
    do i = dim(month) to 1 by -1;
        m_{i+1}  = m_{i};
        end;
    end;
m_{1} = new_demand;
do i = 1 to dim(month);
    month{i} = m_{i+1};
    end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Apr 2017 03:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354673#M82996</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-04-29T03:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Retain Values between a data step using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354685#M83001</link>
      <description>&lt;P&gt;Here is another way:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; existing;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; infile datalines delimiter=',';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; input id $ monthid new_demand month1 month2 month3;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; datalines;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1,1,2,3,4,5&lt;/P&gt;&lt;P&gt;1,2,3,.,.,.&lt;/P&gt;&lt;P&gt;1,3,0,.,.,.&lt;/P&gt;&lt;P&gt;1,4,3,.,.,.&lt;/P&gt;&lt;P&gt;1,5,1,.,.,.&lt;/P&gt;&lt;P&gt;2,1,2,3,4,5&lt;/P&gt;&lt;P&gt;2,2,3,.,.,.&lt;/P&gt;&lt;P&gt;2,3,0,.,.,.&lt;/P&gt;&lt;P&gt;2,4,3,.,.,.&lt;/P&gt;&lt;P&gt;2,5,1,.,.,.&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&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;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; desired;&lt;/P&gt;&lt;P&gt;length&amp;nbsp;&amp;nbsp; _month1 _month2 _month3 &lt;STRONG&gt;8&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;retain &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;_month1 _month2 _month3;&lt;/P&gt;&lt;P&gt;if (_n_ = &lt;STRONG&gt;1&lt;/STRONG&gt;) then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare hash myhash();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc = myhash.definekey('id');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc = myhash.definedata('_month1','_month2','_month3');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; myhash.definedone();&lt;/P&gt;&lt;P&gt;&amp;nbsp;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;set existing;&lt;/P&gt;&lt;P&gt;&amp;nbsp;by id;&lt;/P&gt;&lt;P&gt;&amp;nbsp;if first.id then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;myhash.clear();&lt;/P&gt;&lt;P&gt;_month1= new_demand;&lt;/P&gt;&lt;P&gt;_month2=month1 ;&lt;/P&gt;&lt;P&gt;_month3=month2;&lt;/P&gt;&lt;P&gt;myhash.add();&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;myhash.find();&lt;/P&gt;&lt;P&gt;month1=&amp;nbsp;&amp;nbsp;&amp;nbsp; _month1;&lt;/P&gt;&lt;P&gt;month2=&amp;nbsp;&amp;nbsp;&amp;nbsp; _month2;&lt;/P&gt;&lt;P&gt;month3=&amp;nbsp;&amp;nbsp;&amp;nbsp; _month3;&lt;/P&gt;&lt;P&gt;_month1= new_demand;&lt;/P&gt;&lt;P&gt;_month2=month1 ;&lt;/P&gt;&lt;P&gt;_month3=month2;&lt;/P&gt;&lt;P&gt;myhash.replace();&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop _: rc;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Naveen Srinivasan&lt;/P&gt;</description>
      <pubDate>Sat, 29 Apr 2017 07:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354685#M83001</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2017-04-29T07:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Retain Values between a data step using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354778#M83036</link>
      <description>&lt;P&gt;Here is a method that works for your data. It might not work if observations other than the first on had values in the MONTHx variables.&lt;/P&gt;
&lt;P&gt;Basically convert the first row into 4 observations and the others into 1 and then use LAGn() functions to generate the MONTHn variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by id;
  array val month3-month1 new_demand ;
  do i=1 to dim(val) ;
     value = val(i) ;
     if first.id or i=dim(val) then do;
       new1 = lag1(value);
       new2 = lag2(value);
       new3 = lag3(value);
     end;
  end;
  drop i new_demand month1-month3 ;
  rename value=new_demand new1-new3=month1-month3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Apr 2017 04:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354778#M83036</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-30T04:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Retain Values between a data step using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354779#M83037</link>
      <description>&lt;P&gt;Wow,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;s' code is a rare example of the lag function being put to good use inside a conditional block.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Apr 2017 04:13:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354779#M83037</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-04-30T04:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: How Do I Retain Values between a data step using an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354812#M83051</link>
      <description>&lt;P&gt;Thanks for all the help all, not sure why I was struggling with this so much. I think the temporary array solution gets at what I need the most.&lt;/P&gt;</description>
      <pubDate>Sun, 30 Apr 2017 14:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-Do-I-Retain-Values-between-a-data-step-using-an-array/m-p/354812#M83051</guid>
      <dc:creator>sds2ga</dc:creator>
      <dc:date>2017-04-30T14:22:51Z</dc:date>
    </item>
  </channel>
</rss>

