<?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: Creating lagged variables with underscore in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523527#M142239</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp;var._lag&amp;amp;j = lag&amp;amp;j(&amp;amp;var);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 26 Dec 2018 08:01:35 GMT</pubDate>
    <dc:creator>learsaas</dc:creator>
    <dc:date>2018-12-26T08:01:35Z</dc:date>
    <item>
      <title>Creating lagged variables with underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523525#M142238</link>
      <description>&lt;P&gt;Dear SAS Community:&lt;/P&gt;&lt;P&gt;I already have a sample code for creating lagged variables. However I would like to change the naming of the variables with underscore. Currently the variables are named as&amp;nbsp;lag_&amp;amp;var.&amp;amp;j. I would like to change it to&amp;nbsp;&lt;SPAN&gt;&amp;amp;var_lag&amp;amp;j. So for example if the variable name is&amp;nbsp;age, I would like to have the lagged variable to age_lag1. I tried to change the code but I have problems with the underscores. Thanks much!&lt;/SPAN&gt;&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;%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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Dec 2018 07:25:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523525#M142238</guid>
      <dc:creator>Agent1592</dc:creator>
      <dc:date>2018-12-26T07:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lagged variables with underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523527#M142239</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;amp;var._lag&amp;amp;j = lag&amp;amp;j(&amp;amp;var);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Dec 2018 08:01:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523527#M142239</guid>
      <dc:creator>learsaas</dc:creator>
      <dc:date>2018-12-26T08:01:35Z</dc:date>
    </item>
    <item>
      <title>Re: Creating lagged variables with underscore</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523533#M142241</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 26 Dec 2018 08:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-lagged-variables-with-underscore/m-p/523533#M142241</guid>
      <dc:creator>Agent1592</dc:creator>
      <dc:date>2018-12-26T08:29:20Z</dc:date>
    </item>
  </channel>
</rss>

