<?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 dynamic output in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472007#M120968</link>
    <description>&lt;P&gt;Thanks the answers. i will try to do that.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jun 2018 08:24:34 GMT</pubDate>
    <dc:creator>Aleixo</dc:creator>
    <dc:date>2018-06-21T08:24:34Z</dc:date>
    <item>
      <title>Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471637#M120829</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using this code for import files. This code is a copy from another topic.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path=C:\My_SAS_PROJECTS;
filename folder "&amp;amp;path\Data.";
libname c 'C:\Temp';         /* just an example destination folder */
options validmemname=extend; /* to allow non-standard dataset names */
 
/* Making a list of all files in the folder */
data FilesInFolder;
   length Line 8 File $300;
   List = dopen('folder');  /* corrected the function argument */
   do Line = 1 to dnum(List);
        File = trim(dread(List,Line));
        output;
   end;
   drop list line;
run;
 
/* Creating global macro variables */  /* not "local" */
data _NULL_;
     set FilesInFolder end=final;
     call symput(cats('File', _N_), trim(File));     /* used CATS instead of COMPRESS (...||...) */
     call symput(cats('Name', _N_), trim(nliteral(substr(File,1,min(32, length(File)-4))))); /* inserted */
     if final then call symputx(trim('Total'), _N_); /* replaced symput by symputx */
run;


/* This macro should import all files specified in the list and save them as datasets */
%macro loop;
%do i = 1 %to &amp;amp;Total;
  proc import datafile="&amp;amp;path\Data\&amp;amp;&amp;amp;File&amp;amp;i"
       out=c.&amp;amp;&amp;amp;name&amp;amp;i  /* adapted */
       dbms=xlsx
       replace;
       getnames=yes;
  run;
%end;
%mend loop;
 
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What i want is to named the output with name of file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using this function to cut extension file, like xlsx. It is working for xlsx not for all extensions. But ok fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nopath = substr(trim(dread(List,Line)), 1, length(trim(dread(List,Line))) -5)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* This macro should import all files specified in the list and save them as datasets */
%macro loop;
%do i = 1 %to &amp;amp;Total;
  proc import datafile="&amp;amp;path\Data\&amp;amp;&amp;amp;File&amp;amp;i"
       out=&amp;amp;&amp;amp;Nopath&amp;amp;i  /* adapted */
       dbms=xlsx
       replace;
       getnames=yes;
  run;
%end;
%mend loop;
 
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;72 PROC IMPORT DATAFILE= ""&amp;amp;path\Data\&amp;amp;&amp;amp;File&amp;amp;i" &amp;nbsp;OUT=&amp;amp;&amp;amp;Nopath&amp;amp;i DBMS=XLSX replace; GETNAMES=YES; RUN;&lt;/P&gt;&lt;P&gt;_&lt;BR /&gt;76&lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not understand why is not working and produce several outputs with name of file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 09:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471637#M120829</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-20T09:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471638#M120830</link>
      <description>&lt;P&gt;Your making a mess of code out of a mole hill.&amp;nbsp; You also have errors in your code such as Data. where the dot is invalid.&lt;/P&gt;
&lt;PRE&gt;filename folder "c:/my_sas_projects/data";                    
libname c 'c:/temp';         
options validmemname=extend;   /* Note, bad idea!! */

data _null_;
   length Line 8 File name $300;
   List = dopen('folder');  
   do Line = 1 to dnum(List);
        File = trim(dread(List,Line));
        name=cats('Name', _N_), trim(nliteral(substr(File,1,min(32, length(File)-4))));
        call execute(cats('proc import datafile="c:/my_sas_projects/data/',file,'" out=c.',name,'  dbms=xlsx replace; getnames=yes; run;');
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 10:08:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471638#M120830</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-20T10:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471639#M120831</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro Import;

filename filesloc "path";

%let dirid = %sysfunc(dopen(filesloc));
%let filescnt = %sysfunc(dnum(dirid));

%do i = 1 %to &amp;amp;filescnt;
%let InputFilename = %sysfunc(dread(&amp;amp;dirid,&amp;amp;i));
data _null_;
FileName = scan(&amp;amp;InputFilename,1,'.');
opdataset=substr(Filename,1,min(32,length(Filename)));*DatasetName should be LE 32 Characters;
call symput("OutputDataset",opdataset);
run;
proc import datafile = "path\&amp;amp;InputFileName" dbms = excel out = &amp;amp;outputdataset replace;
run;
%end;

%Mend;

%Import;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 20 Jun 2018 10:15:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471639#M120831</guid>
      <dc:creator>MadhuKorni</dc:creator>
      <dc:date>2018-06-20T10:15:06Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471646#M120837</link>
      <description>&lt;P&gt;Hi, thanks your answer. It is working only for last file. The others not appear.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 11:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471646#M120837</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-20T11:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471648#M120838</link>
      <description>&lt;P&gt;Sorry. My full code of import is this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using pathname(work) because i use "copy files" to put files in the server.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let namefile = %sysfunc(pathname(work));
%put &amp;amp;namefile;

filename pasta "&amp;amp;namefile";
options validmemname=extend; /* to allow non-standard dataset names */

/* Making a list of all files in the pasta */
data Lista_de_ficheiros;
   length Line 8 File_name $300;
   List = dopen('pasta');  /* corrected the function argument */
   do Line = 1 to dnum(List);
        File_name = cats("&amp;amp;namefile", '\', trim(dread(List,Line)));
		Nopath = substr(trim(dread(List,Line)), 1, length(trim(dread(List,Line))) -5);
output;
   end;
   drop list line;
run;

PROC SQL;
   CREATE TABLE WORK.FILTER_FOR_LISTA_DE_FICHEIR_0000 AS 
   SELECT t1.File_name,
   		   t1.Nopath
      FROM WORK.LISTA_DE_FICHEIROS t1
      WHERE t1.File_name CONTAINS '.xlsx';
QUIT;

data INFORMACAO_IMPORTAR_TESTE;
     set WORK.FILTER_FOR_LISTA_DE_FICHEIR_0000 end=final;
     call symput(cats('File_name', _N_), trim(File_name));     /* used CATS instead of COMPRESS (...||...) */
     call symput(cats('Name', _N_), trim(nliteral(substr(File_name,1,min(32, length(File_name)-4))))); /* inserted */
	 call symput("OutputDataset",Nopath);
     if final then call symputx(trim('Total'), _N_); /* replaced symput by symputx */
RUN;

%macro loop;
 %do i = 1 %to &amp;amp;Total;
PROC IMPORT OUT=&amp;amp;OutputDataset
		DATAFILE= "&amp;amp;&amp;amp;File_name&amp;amp;i"
		DBMS=XLSX replace;
		GETNAMES=YES;
RUN;
%end;
 %mend loop;
 
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;No errors but only produce the final file, and the others 4 files are not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What could be the error?&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 11:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471648#M120838</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-20T11:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471653#M120841</link>
      <description>&lt;OL&gt;
&lt;LI&gt;Remove the loop from the macro.&lt;/LI&gt;
&lt;LI&gt;Add parameters for file_name and outputDataset to the macro.&lt;/LI&gt;
&lt;LI&gt;remove call symput calls from the last datastep&lt;/LI&gt;
&lt;LI&gt;call the macro in that datastep using call execute&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 20 Jun 2018 11:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471653#M120841</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-06-20T11:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471709#M120855</link>
      <description>&lt;P&gt;With&lt;/P&gt;
&lt;PRE&gt;72 PROC IMPORT DATAFILE= ""&amp;amp;path\Data\&amp;amp;&amp;amp;File&amp;amp;i"  OUT=&amp;amp;&amp;amp;Nopath&amp;amp;i DBMS=XLSX replace; GETNAMES=YES; RUN;

_
76
ERROR 76-322: Syntax error, statement will be ignored.

 
&lt;/PRE&gt;
&lt;P&gt;The error is because the option GETNAMES is not valid for DBMS=XLSX (or anything not delimited).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the documentation:&lt;/P&gt;
&lt;PRE&gt;The GETNAMES statement is valid only for delimited files. &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW extensive reliance on Proc Import to read multiple files means that you will eventually have problems with&lt;/P&gt;
&lt;P&gt;1) Same named variables that should be of the same type (numeric or character) ending up with different types causing errors when attempting to combine data sets.&lt;/P&gt;
&lt;P&gt;2) Same named character variables with different lengths causing warnings about truncated data when combining data sets and possible loss of data from the truncation&lt;/P&gt;
&lt;P&gt;3) Columns that should have the same variable names in the SAS data set ending up with different actual names for source files that should have the same structure.&lt;/P&gt;
&lt;P&gt;The above warning acquires more importance when using Excel as a file source.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jun 2018 14:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/471709#M120855</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-20T14:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472007#M120968</link>
      <description>&lt;P&gt;Thanks the answers. i will try to do that.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 08:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472007#M120968</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-21T08:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472403#M121130</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not understand point&amp;nbsp; 2. Add parameters. Can you help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let namefile = %sysfunc(pathname(work));
%put &amp;amp;namefile;

filename pasta "&amp;amp;namefile";
options validmemname=extend; /* to allow non-standard dataset names */

/* Making a list of all files in the pasta */
data Lista_de_ficheiros;
   length Line 8 File_name $300;
   List = dopen('pasta');  /* corrected the function argument */
   do Line = 1 to dnum(List);
        File_name = cats("&amp;amp;namefile", '\', trim(dread(List,Line)));
		Nopath = substr(trim(dread(List,Line)), 1, length(trim(dread(List,Line))) -5);
output;
   end;
   drop list line;
run;

PROC SQL;
   CREATE TABLE WORK.FILTER_FOR_LISTA_DE_FICHEIR_0000 AS 
   SELECT t1.File_name,
   		   t1.Nopath
      FROM WORK.LISTA_DE_FICHEIROS t1
      WHERE t1.File_name CONTAINS '.xlsx';
QUIT;

data INFORMACAO_IMPORTAR_TESTE;
     set WORK.FILTER_FOR_LISTA_DE_FICHEIR_0000 end=final;
     call execute(loop);
     if final then call symputx(trim('Total'), _N_); /* replaced symput by symputx */
RUN;

%macro loop;

%let OutputDataset = Nopath;
%let File_name = File_name;
PROC IMPORT OUT=&amp;amp;OutputDataset
		DATAFILE= "&amp;amp;&amp;amp;File_name&amp;amp;i"
		DBMS=XLSX replace;
		GETNAMES=YES;
RUN;
 %mend loop;
 
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I did step 1, 3, and 4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks your attention&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 08:32:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472403#M121130</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-22T08:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472405#M121131</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After that you mean that it is impossible to have an output name dinamic?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I do not understand why&amp;nbsp;OUT=&amp;amp;&amp;amp;Nopath&amp;amp;i&amp;nbsp; is not accept by SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 08:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472405#M121131</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-22T08:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472415#M121132</link>
      <description>&lt;P&gt;Untested code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* define macro before using it */
%macro loop(FileName, OutputDataset);

   proc import out=&amp;amp;OutputDataset.
      datafile= "&amp;amp;FileName."
      dbms=xlsx replace;
   run;

%mend loop;


data INFORMACAO_IMPORTAR_TESTE;
   set work.filter_for_lista_de_ficheir_0000;

   length _DatasetName $ 42;

   _DatasetName = nliteral(substr(File,1,min(32, length(File)-4)));

   call execute(cats('%loop(', File, ',', _DatasetName, ')'));
run;


/*%loop don't call loop here!*/
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jun 2018 09:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472415#M121132</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-06-22T09:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: Import files with dynamic output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472420#M121135</link>
      <description>&lt;P&gt;I don´t how but i solved the problem using a previous code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let path= %sysfunc(pathname(work));
filename folder "&amp;amp;path\";
options validmemname=extend; /* to allow non-standard dataset names */
 
/* Making a list of all files in the folder */
data FilesInFolder;
   length Line 8 File $300;
   List = dopen('folder');  /* corrected the function argument */
   do Line = 1 to dnum(List);
        File = trim(dread(List,Line));
        output;
   end;
   drop list line;
run;

PROC SQL;
   CREATE TABLE WORK.Filtro_de_excel AS 
   SELECT t1.File
      FROM WORK.FilesInFolder t1
      WHERE t1.File CONTAINS '.xlsx';
QUIT;
 
/* Creating global macro variables */  /* not "local" */
data _NULL_;
     set Filtro_de_excel end=final;
     call symput(cats('File', _N_), trim(File));     /* used CATS instead of COMPRESS (...||...) */
     call symput(cats('Name', _N_), trim(nliteral(substr(File,1,min(32, length(File)-4))))); /* inserted */
     if final then call symputx(trim('Total'), _N_); /* replaced symput by symputx */
run;

/* This macro should import all files specified in the list and save them as datasets */
%macro loop;
%do i = 1 %to &amp;amp;Total;
  proc import datafile="&amp;amp;path\&amp;amp;&amp;amp;File&amp;amp;i"
       out=Work.&amp;amp;&amp;amp;name&amp;amp;i  /* adapted */
       dbms=xlsx replace;  
	   getnames=yes;
  run;
%end;
%mend loop;
 
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If someone know why SAS accept this time:&lt;/P&gt;&lt;P&gt;out=Work.&amp;amp;&amp;amp;name&amp;amp;i&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i appreciate that.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jun 2018 09:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-files-with-dynamic-output/m-p/472420#M121135</guid>
      <dc:creator>Aleixo</dc:creator>
      <dc:date>2018-06-22T09:29:53Z</dc:date>
    </item>
  </channel>
</rss>

