<?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: Do Looping into macro in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322142#M21533</link>
    <description>&lt;P&gt;Up to now, you would specify (for example):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var=var1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is&amp;nbsp; your list of variables short enough that&amp;nbsp;you can list all the names, such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var_list = var1 var2 var3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or do you need to automate the part about discovering the names of the variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Secondarily, are all your variable names short enough that you can add "Lead1_" to the beginning of the name without exceeding 32 characters?)&lt;/P&gt;</description>
    <pubDate>Tue, 03 Jan 2017 14:52:15 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-01-03T14:52:15Z</dc:date>
    <item>
      <title>Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322133#M21532</link>
      <description>&lt;P&gt;I am trying to create lead/lag variables from a data set.&amp;nbsp; My goal is to take all variables and create a certain number of lead/lag variables to be added to the data set.&amp;nbsp; I have a macro that creates the lead/lags for one variable.&amp;nbsp; I am having difficulty looping the variables in my data set through the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;* Here is some sample data;
data mydata;
input time var1 var2 var3;
datalines;
1 1.2 54 100
2 1.8 50 90
3 1.5 56 95
4 2.1 67 92
5 2.5 75 110
6 1.9 77 104
7 2.3 84 108
8 2.1 81 112
;
options validvarname=v7;
run;

*Here is part of the macro I created;
%macro laglead(mydata=, var=);
	/*set mydata;*/
	set &amp;amp;mydata(firstobs=2 keep=&amp;amp;var rename=(&amp;amp;var=Lead1_&amp;amp;var) )
		&amp;amp;mydata( obs=1 drop=_all_);

	%let name = Lag1_&amp;amp;var;
	&amp;amp;name = ifn(first.&amp;amp;var, (.), lag(&amp;amp;var));

	%let name = fut_chg1_&amp;amp;var;
	&amp;amp;name = lead1_&amp;amp;var-&amp;amp;var;

	%let name = past_chg1&amp;amp;var;
	&amp;amp;name = &amp;amp;var-lag1(&amp;amp;var);

%mend laglead;

*Loop variables through macro;
data createlead;
	set mydata;

array d var1--var3;
do over d;
%laglead(mydata=mydata,var=d);
end;

run;&lt;/PRE&gt;&lt;P&gt;This returns 2 errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: Variable d is not on file WORK.MYDATA.&lt;/P&gt;&lt;P&gt;ERROR: Invalid DROP, KEEP, or RENAME option on file WORK.MYDATA.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any thoughts or suggestions would be appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using EG 7.13 and yes I have ETS.&amp;nbsp; I couldn't get proc expand to work.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 14:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322133#M21532</guid>
      <dc:creator>Andy5</dc:creator>
      <dc:date>2017-01-03T14:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322142#M21533</link>
      <description>&lt;P&gt;Up to now, you would specify (for example):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var=var1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is&amp;nbsp; your list of variables short enough that&amp;nbsp;you can list all the names, such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var_list = var1 var2 var3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or do you need to automate the part about discovering the names of the variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Secondarily, are all your variable names short enough that you can add "Lead1_" to the beginning of the name without exceeding 32 characters?)&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 14:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322142#M21533</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-03T14:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322154#M21536</link>
      <description>&lt;P&gt;&lt;SPAN class="DateTime lia-message-posted-on lia-component-common-widget-date"&gt;&amp;nbsp;&lt;/SPAN&gt;I would like to be able to automate the discovery of the variable names.&amp;nbsp; I will be using different data sets with different numbers of variables.&amp;nbsp; However, I can easily reorder the variables and do use an array such as var1--var3.&amp;nbsp; Also, I have to do some upfront cleaning of the variable names (removal of spaces) upfront.&amp;nbsp; I can easily trim the names down so I do not run into a character limit issue.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 15:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322154#M21536</guid>
      <dc:creator>Andy5</dc:creator>
      <dc:date>2017-01-03T15:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322166#M21537</link>
      <description>&lt;P&gt;Andy,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Somehow your message got deleted and reappeared, and my solution couldn't be posted.&amp;nbsp; I'll take it in stages this time to make sure I don't lose too much of what I am trying to post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, since you are trying to extract the variable names (only the numeric ones, I assume), you will need to do that ahead of time.&amp;nbsp; It looks like you want&amp;nbsp; your macro to follow the DATA statement, so here is what goes before the DATA statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc contents data=have (keep=_numeric_) out=_contents_ (keep=name) noprint;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;/P&gt;
&lt;P&gt;select strip(name) into : varlist separated by ' ' from _contents_;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That gives&amp;nbsp; you a macro variable with the names of all variables that need to be processed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More to follow ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 15:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322166#M21537</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-03T15:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322170#M21538</link>
      <description>&lt;P&gt;Part 2, changes inside the macro.&amp;nbsp; I'll try to capture your original intentions without changing the formulas too much, but you may need to alter some of the code here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, you need a way to loop through every variable name that needs to be processed.&amp;nbsp; Here is the idea.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%local i varname;&lt;/P&gt;
&lt;P&gt;set &amp;amp;mydata (firstobs=2 keep=&amp;amp;varlist rename=(&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 varname = %scan(&amp;amp;varlist, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;amp;varname = lead1_&amp;amp;varname&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; ) )&amp;nbsp; &amp;amp;mydata (obs=1 drop=_all_);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That would complete the SET statement with all the renames.&amp;nbsp; More to follow ...&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 15:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322170#M21538</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-03T15:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322173#M21539</link>
      <description>&lt;P&gt;Part 3, later changes within the macro.&lt;/P&gt;
&lt;P&gt;Similarly, you will need to loop through every variable for the data manipulation logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to %sysfunc(countw(&amp;amp;varlist));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; %let varname = %scan(&amp;amp;varlist, &amp;amp;i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; lag1_&amp;amp;varname = lag(&amp;amp;varname);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; past_chg1_&amp;amp;varname = dif(&amp;amp;varname);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; fut_chg1_&amp;amp;varname = lead1_&amp;amp;varname - &amp;amp;varname;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may need to confirm that the new variables have the values you intended.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 15:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322173#M21539</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-03T15:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: Do Looping into macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322219#M21544</link>
      <description>&lt;P&gt;This works great!&amp;nbsp; Thank you for all three parts.&amp;nbsp; I don't understand all of the code yet but I will take some time to learn exactly what is going on.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Jan 2017 18:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Do-Looping-into-macro/m-p/322219#M21544</guid>
      <dc:creator>Andy5</dc:creator>
      <dc:date>2017-01-03T18:07:24Z</dc:date>
    </item>
  </channel>
</rss>

