<?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: Macro for lag terms in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339043#M77323</link>
    <description>&lt;P&gt;Note that all of these approaches may have issues with 1) long variable names: adding 4 or more characters to create the new variables means that an existing variable with more than 28 characters could create an invalid variable name and &amp;nbsp;2) could result in collision of names, using one that already exists in the data set.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Mar 2017 22:38:10 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-03-07T22:38:10Z</dc:date>
    <item>
      <title>Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338967#M77281</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to create 4 lag terms each for 15 different variables, is there a way to create macro for 4 lag terms for each of my variable in SAS UE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually, I use Var A1=lag1(VarA);&lt;/P&gt;
&lt;P&gt;for creating lag but what if we have more than 20 variables and need to create more than 4 lag terms?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for any suggestions/advise/cool insights!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;RJ&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 20:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338967#M77281</guid>
      <dc:creator>rj</dc:creator>
      <dc:date>2017-03-07T20:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338979#M77288</link>
      <description>&lt;P&gt;Since your macro would have to create 20*4 (per the example) NEW variables how are you going to use them? How will you know what the names are for other uses? You may need to provide a bit more detail about what the overall goal and further processes for this to get something workable.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 20:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338979#M77288</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-07T20:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338983#M77290</link>
      <description>&lt;P&gt;I'd use proc expand with a macro loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I'd also suggest looking at PROC TIMESERIES to see if it's helpful in this situation.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 20:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338983#M77290</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-07T20:46:20Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338999#M77299</link>
      <description>&lt;P&gt;I don't understand what do you mean by "4 lag terms".&lt;/P&gt;
&lt;P&gt;If you need create lag to many variables you can use next macro, as in this demo:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lag(vars);
  %let m = %sysfunc(countw(&amp;amp;vars)); 
  %do i=1 %to &amp;amp;m;
     %let var = %scan(&amp;amp;vars,&amp;amp;i);
     %do; lag_&amp;amp;var = lag(&amp;amp;var); %end;
  %end;
%mend lag;

data test;
 set sashelp.class;
     %lag(age height name sex);  /* variables to create lag_xxx to var xxx */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Mar 2017 21:05:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/338999#M77299</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-03-07T21:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339006#M77302</link>
      <description>&lt;P&gt;And add another loop/parameter to do more lags.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Mostly stolen from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro lag(vars, lags);
	%let m = %sysfunc(countw(&amp;amp;vars));

	%do i=1 %to &amp;amp;m;
		%let var = %scan(&amp;amp;vars,&amp;amp;i);

		%do j=1 %to &amp;amp;lags;
			%do;
				lag_&amp;amp;var.&amp;amp;j = lag&amp;amp;j(&amp;amp;var);
			%end;
		%end;
	%end;
%mend lag;

data test;
	set sashelp.class;

	%lag(age height name sex, 4);  /* variables to create lag_xxx to var xxx */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Mar 2017 21:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339006#M77302</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-07T21:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339008#M77303</link>
      <description>VarA1=lag1(var A);&lt;BR /&gt;VarA2=lag2(var A);&lt;BR /&gt;VarA3=lag3(var A);&lt;BR /&gt;VarA4=lag4(var A);</description>
      <pubDate>Tue, 07 Mar 2017 21:17:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339008#M77303</guid>
      <dc:creator>rj</dc:creator>
      <dc:date>2017-03-07T21:17:09Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339011#M77305</link>
      <description>I will look up Proc timeseries and proc expand.Thanks!</description>
      <pubDate>Tue, 07 Mar 2017 21:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339011#M77305</guid>
      <dc:creator>rj</dc:creator>
      <dc:date>2017-03-07T21:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339021#M77310</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/58936"&gt;@rj&lt;/a&gt;&amp;nbsp;I modified the macro above to work for multiple lags &amp;amp; multiple variables.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 21:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339021#M77310</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-07T21:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339023#M77311</link>
      <description>&lt;P&gt;You can't get around needing all these statements.&amp;nbsp; But what you can do is have macro language generate them for you.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro all_lags (varlist=);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %local i j nextvar;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %do i=1 %to %sysfunc(countw(&amp;amp;varlist));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %let nextvar = %scan(&amp;amp;varlist, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do j=1 %to 4;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;nextvar&amp;amp;j = lag&amp;amp;j(&amp;amp;nextvar);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;%mend all_lags;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then within the DATA step that creates all 80 additional variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%all_lags (varlist=VarA VarB VarC ....... VarT)&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 21:48:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339023#M77311</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-07T21:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339043#M77323</link>
      <description>&lt;P&gt;Note that all of these approaches may have issues with 1) long variable names: adding 4 or more characters to create the new variables means that an existing variable with more than 28 characters could create an invalid variable name and &amp;nbsp;2) could result in collision of names, using one that already exists in the data set.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Mar 2017 22:38:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339043#M77323</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-07T22:38:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for lag terms</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339086#M77337</link>
      <description>&lt;PRE&gt;
It is a lot easy for IML code.


proc iml;
use sashelp.class;
read all var{age};
close;
lag=lag(age,1:4);
print lag;
quit;


&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Mar 2017 03:15:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-lag-terms/m-p/339086#M77337</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-08T03:15:18Z</dc:date>
    </item>
  </channel>
</rss>

