<?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: Importing 2 datafiles using sas macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640325#M190701</link>
    <description>&lt;P&gt;Well, the two macro calls obviously created the same result (2 datasets, x and y) that your non-macro code achieved. Where is the problem?&lt;/P&gt;</description>
    <pubDate>Thu, 16 Apr 2020 08:40:38 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-04-16T08:40:38Z</dc:date>
    <item>
      <title>Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640250#M190650</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I am new to sas macro and I have been trying to use it to import 2 datafiles but seems I am missing a line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is my code. a1 and a2 are the 2 datafiles. Data_file1 &amp;amp;2 are the file names. Data_dir is the file path for both data. 2 datasets were imported as my output but contain same data (a1).&lt;/P&gt;
&lt;P&gt;Kindly help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro one (a1, a2);
proc import out= &amp;amp;a1 &amp;amp;a2
datafile ="&amp;amp;data_dir\&amp;amp;data_file1"
dbms=xls replace;
getnames=yes;
run;
%mend one;

%one (&amp;amp;library..&amp;amp;a1);
%one (&amp;amp;library..&amp;amp;a2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 16 Apr 2020 02:31:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640250#M190650</guid>
      <dc:creator>aayo</dc:creator>
      <dc:date>2020-04-16T02:31:30Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640259#M190655</link>
      <description>&lt;P&gt;So first you have four undefined macro variables in your little program. Two in the macro definition: DATA_DIR and DATA_FILE1. And two in the calls to the macro:&amp;nbsp;LIBRARY and A1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second both calls are going to read from the same source file since that part of the macro doesn't depend on the input parameters, A1 and A2.&amp;nbsp; Instead it only depends on the values of those two "magic" macro variables.&amp;nbsp; Magic macro variables is my term for macro variables that are referenced in the middle of a program without any indication of where the values coming from. The get values magically.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And finally you are calling the macro but are only supplying a value for one of the two input parameters.&amp;nbsp; But fortunately that doesn't cause any trouble since when the second parameter is empty it just means that there isn't an extra word in the PROC IMPORT statement between the value of the OUT= option and the start of the DATAFILE= option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try something like this instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro one (filename, dataset);
proc import out= &amp;amp;dataset
  datafile =&amp;amp;filename
  dbms=xls replace
;
  getnames=yes;
run;
%mend one;

%let data_path=C:\myfiles ;
options mprint;
%one (filename="&amp;amp;data_path\file1.xls",dataset=work.file1);
%one (filename="&amp;amp;data_path\file2.xls",dataset=work.file2);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Turning on the MPRINT option will let you see what code the macro ends up generating in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 02:42:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640259#M190655</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-16T02:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640278#M190666</link>
      <description>&lt;P&gt;Go back to square 1 of the macro development path: start with working SAS code without any macro elements.&lt;/P&gt;
&lt;P&gt;Please post that code here, and we'll show you how to get from static code to dynamic code in a few steps that are guaranteed to always work.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 06:14:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640278#M190666</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-16T06:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640287#M190674</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;I defined my macros before but didn't post it here. I have tried your suggestion but I still don't understand the first line. Here is my full code now.&lt;/P&gt;&lt;P&gt;%let data_dir = %str(C:\Users\aayo....);&lt;BR /&gt;%let data_file1 = %str(x);&amp;nbsp;&lt;BR /&gt;%let data_file2 = %str(y);&amp;nbsp;&lt;BR /&gt;filename data "&amp;amp;data_dir";&lt;BR /&gt;%let a1 = x;&amp;nbsp;/*set name of sas dataset*/&lt;BR /&gt;%let a2 = y; /*set name of sas dataset*/&lt;BR /&gt;%let c=z;&lt;BR /&gt;%let library = cwork; /* set name of user library */&lt;BR /&gt;libname &amp;amp;library "&amp;amp;data_dir";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro one (x, y);&lt;BR /&gt;proc import out= &amp;amp;a1 &amp;amp;a2&lt;BR /&gt;datafile ="&amp;amp;data_dir"&lt;BR /&gt;dbms=xls replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;BR /&gt;%mend one;&lt;BR /&gt;%one (&amp;amp;data_dir\&amp;amp;data_file1..xls,&amp;amp;library..&amp;amp;a1);&lt;BR /&gt;%one (&amp;amp;data_dir\&amp;amp;data_file2..xls,&amp;amp;library..&amp;amp;a2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 07:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640287#M190674</guid>
      <dc:creator>aayo</dc:creator>
      <dc:date>2020-04-16T07:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640290#M190677</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;below is my working code but i believe i should be able to import the 2 files in one macro statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC IMPORT OUT= WORK.x&amp;nbsp;&lt;BR /&gt;DATAFILE= "C:\Users\aayo\x.xls"&lt;BR /&gt;DBMS=EXCEL REPLACE;&lt;BR /&gt;RANGE="Sheet1$";&lt;BR /&gt;GETNAMES=YES;&lt;BR /&gt;MIXED=NO;&lt;BR /&gt;RUN;&lt;BR /&gt;PROC IMPORT OUT= WORK.y&lt;BR /&gt;DATAFILE= "C:\Users\aayo\y.xls"&lt;BR /&gt;DBMS=EXCEL REPLACE;&lt;BR /&gt;RANGE="Sheet1$";&lt;BR /&gt;GETNAMES=YES;&lt;BR /&gt;MIXED=NO;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 07:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640290#M190677</guid>
      <dc:creator>aayo</dc:creator>
      <dc:date>2020-04-16T07:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640298#M190683</link>
      <description>&lt;P&gt;Step one: identify the parts of the code that need to be dynamic; in your case, it is the name of the Excel file and the name of the dataset, everything else is the same. And the file- and dataset-name are identical, so we can use the same macro variable for both&lt;/P&gt;
&lt;P&gt;So we replace these with a macro variable, and set it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name=x;

proc import
  out=work.&amp;amp;name. 
  datafile="C:\Users\aayo\&amp;amp;name..xls"
  dbms=excel
  replace
;
range="Sheet1$";
getnames=yes;
mixed=no;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note the additional dot in the filename; it is needed to terminate the macro variable reference.&lt;/P&gt;
&lt;P&gt;Once this is tested and verified to work, wrap it into a macro definition, with the macro variable(s) as parameter(s):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro import_one(name);
proc import
  out=work.&amp;amp;name. 
  datafile="C:\Users\aayo\&amp;amp;name..xls"
  dbms=excel
  replace
;
range="Sheet1$";
getnames=yes;
mixed=no;
run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and call this repeatedly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%import_one(x)
%import_one(y)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have a list of parameters for the macro stored in a dataset, you can automate the calls:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
input name $;
datalines;
x
y
;

data _null_;
set list;
call execute(cats('%nrstr(%import_one(',name,'))'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The %nrstr() wrapper is used to prevent macro-timing problems and premature execution of pure macro code.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 07:33:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640298#M190683</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-16T07:33:37Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640319#M190695</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;However, I have 2 different files x (110 obs) and y (236 obs). This code gives me 2 datasets with x attributes and that is where my issue is. how do I state the second filename? Below is the log for clearer picture.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;234 %one (&amp;amp;a1);&lt;/P&gt;&lt;P&gt;NOTE: The import data set has 110 observations and 8 variables.&lt;BR /&gt;NOTE: WORK.X data set was successfully created.&lt;BR /&gt;NOTE: PROCEDURE IMPORT used (Total process time):&lt;BR /&gt;real time 0.02 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;235 %one (&amp;amp;a2);&lt;/P&gt;&lt;P&gt;NOTE: The import data set has 110 observations and 8 variables.&lt;BR /&gt;NOTE: WORK.Y data set was successfully created.&lt;BR /&gt;NOTE: PROCEDURE IMPORT used (Total process time):&lt;BR /&gt;real time 0.01 seconds&lt;BR /&gt;cpu time 0.00 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 08:17:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640319#M190695</guid>
      <dc:creator>aayo</dc:creator>
      <dc:date>2020-04-16T08:17:27Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640325#M190701</link>
      <description>&lt;P&gt;Well, the two macro calls obviously created the same result (2 datasets, x and y) that your non-macro code achieved. Where is the problem?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 08:40:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640325#M190701</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-16T08:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640333#M190708</link>
      <description>&lt;P&gt;Below is my non-macro log.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;287 PROC IMPORT OUT= WORK.x&lt;BR /&gt;288 DATAFILE= "C:\Users\aayo\x.xls"&lt;BR /&gt;290 DBMS=EXCEL REPLACE;&lt;BR /&gt;291 RANGE="Sheet1$";&lt;BR /&gt;292 GETNAMES=YES;&lt;BR /&gt;293 MIXED=NO;&lt;BR /&gt;294 RUN;&lt;/P&gt;&lt;P&gt;NOTE: WORK.x data set was successfully created.&lt;BR /&gt;NOTE: The data set WORK.x has 110 observations and 8 variables.&lt;BR /&gt;NOTE: PROCEDURE IMPORT used (Total process time):&lt;BR /&gt;real time 1.48 seconds&lt;BR /&gt;cpu time 0.37 seconds&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;298 PROC IMPORT OUT= WORK.y&lt;BR /&gt;299 DATAFILE= "C:\Users\aayo\y.xls"&lt;BR /&gt;301 DBMS=EXCEL REPLACE;&lt;BR /&gt;302 RANGE="Sheet1$";&lt;BR /&gt;303 GETNAMES=YES;&lt;BR /&gt;304 MIXED=NO;&lt;BR /&gt;305 RUN;&lt;/P&gt;&lt;P&gt;NOTE: WORK.y data set was successfully created.&lt;BR /&gt;NOTE: The data set WORK.y has 263 observations and 8 variables.&lt;BR /&gt;NOTE: PROCEDURE IMPORT used (Total process time):&lt;BR /&gt;real time 0.37 seconds&lt;BR /&gt;cpu time 0.21 seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The missing line in the macro I believe is specifying the 2nd filename.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 08:51:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640333#M190708</guid>
      <dc:creator>aayo</dc:creator>
      <dc:date>2020-04-16T08:51:06Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640336#M190711</link>
      <description>&lt;P&gt;Compare your macro code to mine. You &lt;EM&gt;will&lt;/EM&gt; spot the difference.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 08:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640336#M190711</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-16T08:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640397#M190746</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;I got it now and I think I am getting to understand how macro works to reduce repetitive codes.&lt;/P&gt;&lt;P&gt;Thanks for your patience.&lt;/P&gt;&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 11:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640397#M190746</guid>
      <dc:creator>aayo</dc:creator>
      <dc:date>2020-04-16T11:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Importing 2 datafiles using sas macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640407#M190750</link>
      <description>&lt;P&gt;The most important think to keep in mind: macro is a code generator, so the code to be created has to be at the center of the coder's attention.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Apr 2020 11:58:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Importing-2-datafiles-using-sas-macro/m-p/640407#M190750</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-04-16T11:58:57Z</dc:date>
    </item>
  </channel>
</rss>

