<?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 Creating lags of multiple variables using an array. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285508#M58392</link>
    <description>&lt;P&gt;I'm trying to find&amp;nbsp; a way to efficiently loop through a large number of variables (~50), and lags 1-4 of each variable.&amp;nbsp; I have tried to do this using an array:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
array lags(*) x1 x2 x3;
set full;
do i=1 to dim(lags);
lags(i)=lag1(lags(i));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This will work, but it replaces the orginial variables with a lag, but the they all have the original names. What I'm hoping to achieve:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1). All data in one work file.&lt;/P&gt;&lt;P&gt;2). Lags 1-4 of all variables&lt;/P&gt;&lt;P&gt;3).&amp;nbsp;Variables renamed to represent their respective lag order ex: x1L1, x1L2, x1L3, x1L4...&lt;/P&gt;</description>
    <pubDate>Tue, 19 Jul 2016 12:57:33 GMT</pubDate>
    <dc:creator>dsriggs</dc:creator>
    <dc:date>2016-07-19T12:57:33Z</dc:date>
    <item>
      <title>Creating lags of multiple variables using an array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285508#M58392</link>
      <description>&lt;P&gt;I'm trying to find&amp;nbsp; a way to efficiently loop through a large number of variables (~50), and lags 1-4 of each variable.&amp;nbsp; I have tried to do this using an array:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
array lags(*) x1 x2 x3;
set full;
do i=1 to dim(lags);
lags(i)=lag1(lags(i));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This will work, but it replaces the orginial variables with a lag, but the they all have the original names. What I'm hoping to achieve:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1). All data in one work file.&lt;/P&gt;&lt;P&gt;2). Lags 1-4 of all variables&lt;/P&gt;&lt;P&gt;3).&amp;nbsp;Variables renamed to represent their respective lag order ex: x1L1, x1L2, x1L3, x1L4...&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 12:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285508#M58392</guid>
      <dc:creator>dsriggs</dc:creator>
      <dc:date>2016-07-19T12:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lags of multiple variables using an array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285518#M58394</link>
      <description>&lt;P&gt;Sorry, I don't see that being a good structure to work with. &amp;nbsp;Why do you need such a structure as your just going to make a lot of work for yourself going forward, if you already have 50 variables (which is an awful lot) then add another 200 variables etc. &amp;nbsp;How are you going to program with this? &amp;nbsp;What I would suggest is to normalise you existing data, so those 50 variables go down the table in the form of paramter value response - the lag is then implicit in the data list:&lt;/P&gt;
&lt;P&gt;From:&lt;/P&gt;
&lt;P&gt;var1 var2 var3&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp;b &amp;nbsp; &amp;nbsp; &amp;nbsp; c&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To:&lt;/P&gt;
&lt;P&gt;var &amp;nbsp;result&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; a&lt;/P&gt;
&lt;P&gt;2 &amp;nbsp; &amp;nbsp; b&lt;/P&gt;
&lt;P&gt;3 &amp;nbsp; &amp;nbsp; c&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Let me give another example. &amp;nbsp;In your new structure, for each record you will duplicate this 4 times onto subsequent rows, hence your dataset size is going to grow quite a bit - and it will not contain any new information as its just copies. &amp;nbsp;If you normalise your data, add a sequence variable, you can very easily merge data, &amp;nbsp;so if you want previous row it is a.var=b.var and a.seq=b.seq-1.&lt;/P&gt;
&lt;P&gt;So the data remains minmal, but has the option of merging to get lags.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 13:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285518#M58394</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-07-19T13:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lags of multiple variables using an array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285530#M58397</link>
      <description>&lt;P&gt;What I am trying to program with this is see if there's a good combination of various lags that have predictive power in a regression modeling framework.&amp;nbsp; I was hoping to run the variables through a stepwise algorithim.&amp;nbsp; I have done this already with the variables with no lags, but I was going to try again to see if a combination of various lagged variables provides any benefit.&amp;nbsp; I wouldn't think it would be too difficult to work within the structure I started if there was just&amp;nbsp; a way I could rename all variables in the array to have the lag specific suffix.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 14:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285530#M58397</guid>
      <dc:creator>dsriggs</dc:creator>
      <dc:date>2016-07-19T14:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lags of multiple variables using an array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285535#M58398</link>
      <description>&lt;P&gt;The easiest way would be to use generic variable names.&amp;nbsp; After the analysis, take whatever names pop out as significant and find out what they mean by comparing them to the original program.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array original {50} list all 50 names here for the existing variables;&lt;/P&gt;
&lt;P&gt;array L1_ {50};&lt;/P&gt;
&lt;P&gt;array L2_ {50};&lt;/P&gt;
&lt;P&gt;array L3_ {50};&lt;/P&gt;
&lt;P&gt;array L4_ {50};&lt;/P&gt;
&lt;P&gt;do _n_=1 to 50;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; L1_{_n_} = lag1(original{_n_});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; L2_{_n_} = lag2(original{_n_});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; L3_{_n_} = lag3(original{_n_});&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; L4_{_n_} = lag4(original{_n_});&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if L3_47 pops out in the regression, you will need to go back to the original program and find the name of the 47th original variable.&amp;nbsp; There are ways to automate this, but they have pitfalls and wouldn't be appropriate for a one-time analysis.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2016 14:41:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285535#M58398</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-19T14:41:58Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lags of multiple variables using an array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285712#M58472</link>
      <description>&lt;P&gt;Actually it is IML thing .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lag1;
output;
set sashelp.class;
keep name age;
rename name=name_lag1 age=age_lag1;
run;
data lag2;
output;
output;
set sashelp.class;
keep name age;
rename name=name_lag2 age=age_lag2;
run;
data lag3;
output;
output;
output;
set sashelp.class;
keep name age;
rename name=name_lag3 age=age_lag3;
run;
data lag4;
output;
output;
output;
output;
set sashelp.class;
keep name age;
rename name=name_lag4 age=age_lag4;
run;


data want;
 merge sashelp.class(keep=name age in=ina) lag:;
 if ina;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jul 2016 02:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285712#M58472</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-20T02:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lags of multiple variables using an array.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285719#M58475</link>
      <description>&lt;P&gt;Actually It is IML thing .&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;proc iml;
use sashelp.class;
read all var _num_ into x[c=vnames];
close;

do i=1 to ncol(x);
 temp=vnames[i];
 names=temp||(strip(temp)+'_lag1':strip(temp)+'_lag4');
 want=lag(x[,i],0:4);

 create ('want_'+temp) from want[c=names];
 append from want;
 close;
end;

quit;

data final_want;
 merge want_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jul 2016 02:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lags-of-multiple-variables-using-an-array/m-p/285719#M58475</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-20T02:42:55Z</dc:date>
    </item>
  </channel>
</rss>

