<?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:  Import Files with Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311922#M67553</link>
    <description>&lt;P&gt;%do i = 1 %to 60;&lt;/P&gt;
&lt;P&gt;%do also needs a % with the "to".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have filename as macro parameter, but set it explicitly in the data _null_, so the macro parameter is useless.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%if filename=filexist("&amp;amp;&amp;amp;loc.&amp;amp;filename.xls");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;This would always be semantically wrong (filename is a string, the result of the function is boolean/numeric), but in a macro you can't use datastep functions without %sysfunc.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And there is no %then!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Since you never use i in the macro loop (or change anything), you would just repeat the same action 60 times.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What were you trying to achieve, anyway?&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Nov 2016 08:07:34 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-11-16T08:07:34Z</dc:date>
    <item>
      <title>Import Files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311915#M67551</link>
      <description>&lt;P&gt;i have a list of excel files to be imported and has to be consolidated in a big file.&lt;/P&gt;
&lt;P&gt;the file name are in the format customer data file 2016-04,2016-03 and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;first i have tried&lt;/P&gt;
&lt;P&gt;%MACRO import(filename);&lt;BR /&gt;proc import datafile="&amp;amp;&amp;amp;loc&amp;amp;filename "&lt;BR /&gt;out=cust%sysfunc(compress(&amp;amp;filename.))&lt;BR /&gt;dbms=excel replace;&lt;BR /&gt;run;&lt;BR /&gt;%MEND;&lt;/P&gt;
&lt;P&gt;%import(2016-14);&lt;BR /&gt;proc print;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but i want to automate it further and thats where i am getting stuck&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let loc=C:\Users\user\Downloads\New folder\Customer Data File;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%MACRO importfile(filename);&lt;/P&gt;
&lt;P&gt;%do i=1 to 60;&lt;BR /&gt;data _null_;&lt;/P&gt;
&lt;P&gt;data files;&lt;BR /&gt;callsymput("filename",put("042016"d, yymm7.));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;%if filename=filexist("&amp;amp;&amp;amp;loc.&amp;amp;filename.xls");&lt;/P&gt;
&lt;P&gt;proc import datafile="&amp;amp;&amp;amp;loc. &amp;amp;filename"&lt;BR /&gt;out=c%sysfunc(compress(&amp;amp;filename.))&lt;BR /&gt;dbms= excel replace;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data final;&lt;BR /&gt;set final&amp;amp;filename. ;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%MEND;&lt;/P&gt;
&lt;P&gt;%importfile(04-2016);&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>Wed, 16 Nov 2016 08:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311915#M67551</guid>
      <dc:creator>basabi</dc:creator>
      <dc:date>2016-11-16T08:05:05Z</dc:date>
    </item>
    <item>
      <title>Re:  Import Files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311922#M67553</link>
      <description>&lt;P&gt;%do i = 1 %to 60;&lt;/P&gt;
&lt;P&gt;%do also needs a % with the "to".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have filename as macro parameter, but set it explicitly in the data _null_, so the macro parameter is useless.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%if filename=filexist("&amp;amp;&amp;amp;loc.&amp;amp;filename.xls");&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;This would always be semantically wrong (filename is a string, the result of the function is boolean/numeric), but in a macro you can't use datastep functions without %sysfunc.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And there is no %then!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Since you never use i in the macro loop (or change anything), you would just repeat the same action 60 times.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What were you trying to achieve, anyway?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 08:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311922#M67553</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-16T08:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311924#M67554</link>
      <description>&lt;P&gt;Am I right -&lt;/P&gt;
&lt;P&gt;you are trynig to import 60 months (5 years) &amp;nbsp;files, each file name is in a format of yyyy-mm.&lt;/P&gt;
&lt;P&gt;You need to define last (current) month to start from and loop backwards by month 60 times.&lt;/P&gt;
&lt;P&gt;Then you want to append each month data to the previous acuumulated data, to get one consolidated table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try next code, using your macro &lt;STRONG&gt;%import&lt;/STRONG&gt; as is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%let loc=C:\Users\user\Downloads\New folder\Customer Data File; &amp;nbsp;/* assumed full &lt;STRONG&gt;path name only&lt;/STRONG&gt; */&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;%macro &lt;STRONG&gt;import_files&lt;/STRONG&gt;(start=2016-04, libout=, cons_name=);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;/* initiating the consolidated table */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; proc datasets lib=&amp;amp;libout nolist; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* library for the consolidated table */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; delete &amp;amp;cons_name; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* name for the consolidated table */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; run; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* ignore message like &lt;EM&gt;cons_name&amp;nbsp;&lt;/EM&gt;does not exist */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let fname = &amp;amp;start;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %do i=1 &lt;STRONG&gt;%to&lt;/STRONG&gt;&amp;nbsp; 3 ; &amp;nbsp; &amp;nbsp;/* 3 times just for &lt;STRONG&gt;test&lt;/STRONG&gt;, then if OK change to 60 */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%let path_name =&amp;nbsp;&lt;STRONG&gt;&amp;amp;loc.\&amp;amp;fname..xls; &lt;/STRONG&gt;&amp;nbsp;/* pay attention to changes done */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%if %sysfunc(&lt;SPAN&gt;filexist(path_name)) %then %do; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%import(&amp;amp;fname.&lt;STRONG&gt;.xls&lt;/STRONG&gt;);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; proc append base=&amp;amp;cons_name data=cust&amp;amp;fname; run; &amp;nbsp; /* output name as created by %import */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* next fname is previous month */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;data _NULL_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;datex = compress("&amp;amp;fname.01", '-');&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date &amp;nbsp;= input(datex,yymmdd8.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;prev_month = intnx('month',date,-1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new_fname = catx('-',left(year(prev_month)) , put(month(prev_year),z2.));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;call symput('fname', strip(new_name));&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %end;&lt;/P&gt;
&lt;P&gt;%mend&amp;nbsp;&lt;STRONG&gt;import_files;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%import_files(&lt;SPAN&gt;start=2016-04, libout=&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;EM&gt;work&lt;/EM&gt;&lt;STRONG&gt;&lt;SPAN&gt;, cons_name=&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;EM&gt;cons_data&lt;/EM&gt;&lt;STRONG&gt;&lt;SPAN&gt;);&lt;/SPAN&gt;&lt;/STRONG&gt;&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 08:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311924#M67554</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-16T08:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311926#M67555</link>
      <description>&lt;P&gt;you may need amend your %import macro&lt;BR /&gt;&lt;BR /&gt;proc import datafile=&lt;STRONG&gt;"&amp;amp;loc.\&amp;amp;filename..xls"&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 08:21:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311926#M67555</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-16T08:21:14Z</dc:date>
    </item>
    <item>
      <title>Re: SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311940#M67562</link>
      <description>yes exactly..but i want to automate that out datafile name so that the last&lt;BR /&gt;part of it is attached with an "&amp;amp;i." so that it iterates60 times.&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Wed, 16 Nov 2016 09:23:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311940#M67562</guid>
      <dc:creator>basabi</dc:creator>
      <dc:date>2016-11-16T09:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311942#M67564</link>
      <description>&lt;P&gt;You have several good examples above. I wrote this answer a few days ago for DBF files but it can easily be modified for XLSX files, modify the proc import in the macro.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would need to create a dataset with the filenames and output dataset name to pass to the macro but that's an easy step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So a standard proc import would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc import out=sample1 datafile="path to dbf file.dbf" dbms=DBF replace; &lt;BR /&gt; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem now, is how to generate this set of code for every file in your file list. Using the CALL EXECUTE statement from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; is your best bet. You call also create a small macro and call it for each filename, using CALL EXECUTE. If you're new to SAS this can be easier to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*Create a macro that imports the DBF&lt;/P&gt;
&lt;P&gt;%macro import_dbf(input= , output=);&lt;/P&gt;
&lt;P&gt;proc import out=&amp;amp;out datafile="&amp;amp;output" dbms=DBF replace; &lt;BR /&gt; run;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Then call macro from dataset. I'm naming the datasets DBF001, DBF0002 etc.&lt;/P&gt;
&lt;P&gt;%let dirname=C:\_localdata;&lt;BR /&gt; &lt;BR /&gt; data dirlist;&lt;BR /&gt; informat fname $20.;&lt;BR /&gt; input fname;&lt;BR /&gt; cards;&lt;BR /&gt; data1.dbf&lt;BR /&gt; data2.dbf&lt;BR /&gt; data3.dbf&lt;BR /&gt; data4.dbf&lt;BR /&gt; ;&lt;BR /&gt; run;&lt;BR /&gt; &lt;BR /&gt; data out;&lt;BR /&gt; set dirlist;&lt;BR /&gt; str=catt('%import_dbf(input="', "&amp;amp;dirname", '\', fname, '", output=dbf', &lt;BR /&gt; put(_n_, z4.), ');');&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;call execute(str);&lt;BR /&gt; run;&lt;BR /&gt; &lt;BR /&gt; proc print data=out;&lt;BR /&gt; run;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 09:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311942#M67564</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-16T09:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311943#M67565</link>
      <description>&lt;P&gt;Your import macro is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO import(filename);
proc import datafile="&amp;amp;loc.\&amp;amp;filename "
out=cust%sysfunc(compress(&amp;amp;filename.))
dbms=excel replace;
run;
%MEND;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then you need change one line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;STRONG&gt;out=cust&amp;amp;filename&amp;amp;i &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/STRONG&gt;/* I think that %sysfunc(compress - are not needed */&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 09:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/311943#M67565</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-11-16T09:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Import Files with Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/312034#M67599</link>
      <description>&lt;P&gt;Be prepared to correct issues of variable type mismatches and different lengths of character variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc import relies on Excel to tell SAS the variable type and size and is notorious for what human's think of errors.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2016 16:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-Files-with-Macro/m-p/312034#M67599</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-16T16:38:21Z</dc:date>
    </item>
  </channel>
</rss>

