<?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: Using a macro var list within another macro in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342098#M22643</link>
    <description>&lt;P&gt;Create two macro variable lists, one with the old variable names and one with the new variable names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use an array to do the conversion NOT macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array old_vars(*) &amp;amp;list_old_vars;
array new_vars(*) &amp;amp;list_new_vars;

do i=1 to dim(old_vars);

    new_vars(i) = input(old_vars(i), mmddyy10.);

end;

drop &amp;amp;list_old_vars;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 17 Mar 2017 18:10:24 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-03-17T18:10:24Z</dc:date>
    <item>
      <title>Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342090#M22641</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was wondering if I could get&amp;nbsp;SAS code to put a list of values (created into a SQL macro) into a SAS macro.&amp;nbsp; For example below, I want to format all of the date variables in a dataset. I'm in SAS EG (I believe on 9.3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Create a list of variables named "var_list"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
   select distinct date_variables into : var_list separated by ' '
   from dataset
;quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;This creates a list of date variables, for example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;date_1, date_2, … , date_n&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) Set up your macro to format a new variable (with ‘_fmt’ at the end, then rename it as original variable name)&lt;/P&gt;&lt;PRE&gt;%macro dateformat(var);
   &amp;amp;var._fmt = input(&amp;amp;var., MMDDYY10.) ;
   drop &amp;amp;var.; 
   rename &amp;amp;var._fmt = &amp;amp;var.;
%macro;&lt;/PRE&gt;&lt;P&gt;3) Now I want to put all the variables created in step 1) into step 2) in one step instead of doing this multi-step process:&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;dateformat&lt;/EM&gt;&lt;/STRONG&gt;(date_1);&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;dateformat&lt;/EM&gt;&lt;/STRONG&gt;(date_2);&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;%&lt;STRONG&gt;&lt;EM&gt;dateformat&lt;/EM&gt;&lt;/STRONG&gt;(date_n);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do just one macro for the entire list?&amp;nbsp; I’m guessing it something like &lt;STRONG&gt;%dateformat&lt;/STRONG&gt;&lt;STRONG&gt;&lt;EM&gt;(var_list)&lt;/EM&gt;&lt;/STRONG&gt;? But that doesn’t work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 18:03:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342090#M22641</guid>
      <dc:creator>mikepep21</dc:creator>
      <dc:date>2017-03-17T18:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342096#M22642</link>
      <description>&lt;P&gt;Let step 2 process the list.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dateformat(arg);
   %local i var;
   %let i = 1;
   %let var = %scan(&amp;amp;arg,&amp;amp;i,%str( ));
   %do %while(%superq(var) ne);
      &amp;amp;var._fmt = input(&amp;amp;var., MMDDYY10.) ;
      drop &amp;amp;var.;
      rename &amp;amp;var._fmt = &amp;amp;var.;
      %let i = %eval(&amp;amp;i + 1);
      %let var = %scan(&amp;amp;arg,&amp;amp;i,%str( ));
      %end;
   %mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;2&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 18:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342096#M22642</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-03-17T18:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342098#M22643</link>
      <description>&lt;P&gt;Create two macro variable lists, one with the old variable names and one with the new variable names.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then use an array to do the conversion NOT macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array old_vars(*) &amp;amp;list_old_vars;
array new_vars(*) &amp;amp;list_new_vars;

do i=1 to dim(old_vars);

    new_vars(i) = input(old_vars(i), mmddyy10.);

end;

drop &amp;amp;list_old_vars;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 18:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342098#M22643</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-17T18:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342099#M22644</link>
      <description>&lt;P&gt;When you have a dataset with variable names, you can create the conversion step dynamically from that dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set varnames end=done;
if _n_ = 1 then call execute('data want;set have;');
call execute('%dateformat(`!!trim(variable_name)!!');`);
if done then call execute('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 18:11:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342099#M22644</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-17T18:11:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342151#M22645</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Convert character dates to numeric dates with the same name

HAVE
====

 -- CHARACTER --
DATE1           C    9
DATE2           C    9
DATE3           C    9

 -- NUMERIC --
XN              N    8

Up to 40 obs WORK.HAVE total obs=8

Obs    XN      DATE1         DATE2         DATE3

 1     10    01/01/2000    02/21/2000    02/28/2000
 2     20    01/02/2000    02/08/2000    02/25/2000
 3     30    01/03/2000    03/29/2000    03/26/2000
 4     40    01/04/2000    01/15/2000    01/30/2000
 5     50    01/05/2000    02/08/2000    01/15/2000
 6     60    01/06/2000    02/19/2000    03/09/2000
 7     70    01/07/2000    01/21/2000    03/16/2000
 8     80    01/08/2000    02/17/2000    04/03/2000


WANT  (Dates are numeric)
=========================

 -- NUMERIC --
DATE1         N    8
DATE2         N    8
DATE3         N    8
XN            N    8

Obs    XN         DATE1         DATE2         DATE3

 1     10    01/01/2000    02/21/2000    02/28/2000
 2     20    01/02/2000    02/08/2000    02/25/2000
 3     30    01/03/2000    03/29/2000    03/26/2000
 4     40    01/04/2000    01/15/2000    01/30/2000
 5     50    01/05/2000    02/08/2000    01/15/2000
 6     60    01/06/2000    02/19/2000    03/09/2000
 7     70    01/07/2000    01/21/2000    03/16/2000
 8     80    01/08/2000    02/17/2000    04/03/2000


WORKING CODE
============

   ,input( DATE1 ,mmddyy10.) as DATE1
   ,input( DATE2 ,mmddyy10.) as DATE2
   ,input( DATE3 ,mmddyy10.) as DATE3

FULL SOLUTION
=============

* create some data;
data have(keep=xn date1 date2 date3);
  set sashelp.GNGSMP2;
  date1=put(date,mmddyy10.);
  date2=put(date + int(100*uniform(5831)),mmddyy10.);
  date3=put(datepart(datetime) + int(100*uniform(5831)),mmddyy10.);
run;quit;


data _null_;
   length sql $4096.;
   set have;
   array dte date:;
   do over dte;
      nam=vname(dte);
      cmd=catx(' ','input(',nam,',mmddyy10.) as',nam);
      sql=catx(',',sql,cmd);
   end;
   call symputx('sql',sql);
run;quit;

/*
%put &amp;amp;=sql;

SQL=input( DATE1 ,mmddyy10.) as DATE1
,input( DATE2 ,mmddyy10.) as DATE2
,input( DATE3 ,mmddyy10.) as DATE3
*/

proc sql;
   create
      table want as
   select
      xn
     ,&amp;amp;sql
   from
      have
;quit;

proc print data=want;
format date: mmddyy10.;
run;quit;

Up to 40 obs from want total obs=8

Obs    XN         DATE1         DATE2         DATE3

 1     10    01/01/2000    02/21/2000    02/28/2000
 2     20    01/02/2000    02/08/2000    02/25/2000
 3     30    01/03/2000    03/29/2000    03/26/2000
 4     40    01/04/2000    01/15/2000    01/30/2000
 5     50    01/05/2000    02/08/2000    01/15/2000
 6     60    01/06/2000    02/19/2000    03/09/2000
 7     70    01/07/2000    01/21/2000    03/16/2000
 8     80    01/08/2000    02/17/2000    04/03/2000


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 20:34:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342151#M22645</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-17T20:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342631#M22684</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My example of the variables (date1, date2, ..., daten) should actually be more like (date1, second_date, the_third_date, dt_exited...). &amp;nbsp;In other words, the variables wouldn't necessarily end with a number nor look the same. &amp;nbsp;Would this affect the code you have? &amp;nbsp;Sorry I haven't had a chance to try it out, I changed jobs and waiting to get a SAS license.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm really looking for is to have some sort of loop in a data step, not necessarily just formats &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 15:10:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342631#M22684</guid>
      <dc:creator>mikepep21</dc:creator>
      <dc:date>2017-03-20T15:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using a macro var list within another macro</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342636#M22685</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133847"&gt;@mikepep21&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My example of the variables (date1, date2, ..., daten) should actually be more like (date1, second_date, the_third_date, dt_exited...). &amp;nbsp;In other words, the variables wouldn't necessarily end with a number nor look the same. &amp;nbsp;Would this affect the code you have? &amp;nbsp;Sorry I haven't had a chance to try it out, I changed jobs and waiting to get a SAS license.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I'm really looking for is to have some sort of loop in a data step, not necessarily just formats &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You create a blank delimited list of variable names with PROC SQL INTO.&amp;nbsp; This list of names when passed to the macro will be scanned to generate the code to create the new variable.&amp;nbsp; One thing that could be a problem is your macro used the old-name+_FMT as the name of the new variable which could depending on the length of old name produce an invalid SAS name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This modification uses _&amp;amp;i as the temporary name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dateformat(arg);
   %local i var;
   %let i = 1;
   %let var = %scan(&amp;amp;arg,&amp;amp;i,%str( ));
   %do %while(%superq(var) ne);
      _&amp;amp;i = input(&amp;amp;var., MMDDYY10.) ;
      drop &amp;amp;var.; 
      rename _&amp;amp;i = &amp;amp;var.;
      %let i = %eval(&amp;amp;i + 1);
      %let var = %scan(&amp;amp;arg,&amp;amp;i,%str( ));
      %end;
   %mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Mar 2017 15:22:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Using-a-macro-var-list-within-another-macro/m-p/342636#M22685</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2017-03-20T15:22:54Z</dc:date>
    </item>
  </channel>
</rss>

