<?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: Loop over files for cimport and export in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613460#M18419</link>
    <description>&lt;P&gt;Do you have a list of the CPORT files? Do you have the list in a dataset?&lt;/P&gt;
&lt;P&gt;Are you certain that each CPORT file only contains one dataset? Do any of them contain multiple datasets? Or other SAS objects, like format catalogs? When there is only one dataset are you sure the name of the dataset matches the name of the CPORT file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that each file has one and only one dataset and you have the list in a variable named FILENAME in a dataset named FILELIST you could build a macro and then call the macro once for file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro convert_one
(infile=
,outdir=
,outfile=
,libref=sasfiles
,memname=
);
%if not %length(&amp;amp;memname) %then %let memname=%scan(&amp;amp;infile,-2,'./"\');
%if not %length(&amp;amp;outfile) %then %let outfile=&amp;amp;memname..csv;
filename intrans "&amp;amp;infile";
proc cimport infile=intrans library=&amp;amp;libref;
run;
proc export data=&amp;amp;libref..&amp;amp;memname file="&amp;amp;outdir/&amp;amp;outfile" dbms=csv; 
run;
%mend convert_one;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your program to actually convert all of the files can just be generated from the list of files.&amp;nbsp; For example by using a data step and CALL EXECUTE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname sasfiles 'C:\Users\Joaquin\Desktop';
%let outdir=C:\Users\Joaquin\Desktop\;

data _null_;
  set filelist ;
  call execute(cats('%nrstr(%convert_one)('
  ,'infile=',filename
  ,'outdir=',"&amp;amp;outdir"
 &amp;nbsp;,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 22 Dec 2019 23:07:21 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-12-22T23:07:21Z</dc:date>
    <item>
      <title>Loop over files for cimport and export</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613364#M18403</link>
      <description>&lt;P&gt;I am running the following code:&lt;/P&gt;&lt;PRE&gt;libname sasfiles 'C:\Users\Joaquin\Desktop';
filename intrans 'C:\Users\Joaquin\Desktop\zipcode_q1_2005.cpt' ;
proc cimport infile=intrans library=sasfiles;
run;

proc export data=sasfiles.zipcode_q1_2005
    outfile='C:\Users\Joaquin\Desktop\zipcode_q1_2005.csv'
    dbms=csv
    replace;
run;&lt;/PRE&gt;&lt;P&gt;I need to do this for many files (zipcode_q1_2005). At the end, for each cpt I have a csv file.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Dec 2019 00:04:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613364#M18403</guid>
      <dc:creator>jsaldain33</dc:creator>
      <dc:date>2019-12-21T00:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over files for cimport and export</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613416#M18412</link>
      <description>&lt;P&gt;Convert it to a macro and run it for each file needed.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304091"&gt;@jsaldain33&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am running the following code:&lt;/P&gt;
&lt;PRE&gt;libname sasfiles 'C:\Users\Joaquin\Desktop';
filename intrans 'C:\Users\Joaquin\Desktop\zipcode_q1_2005.cpt' ;
proc cimport infile=intrans library=sasfiles;
run;

proc export data=sasfiles.zipcode_q1_2005
    outfile='C:\Users\Joaquin\Desktop\zipcode_q1_2005.csv'
    dbms=csv
    replace;
run;&lt;/PRE&gt;
&lt;P&gt;I need to do this for many files (zipcode_q1_2005). At the end, for each cpt I have a csv file.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Dec 2019 02:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613416#M18412</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-22T02:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loop over files for cimport and export</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613460#M18419</link>
      <description>&lt;P&gt;Do you have a list of the CPORT files? Do you have the list in a dataset?&lt;/P&gt;
&lt;P&gt;Are you certain that each CPORT file only contains one dataset? Do any of them contain multiple datasets? Or other SAS objects, like format catalogs? When there is only one dataset are you sure the name of the dataset matches the name of the CPORT file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that each file has one and only one dataset and you have the list in a variable named FILENAME in a dataset named FILELIST you could build a macro and then call the macro once for file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro convert_one
(infile=
,outdir=
,outfile=
,libref=sasfiles
,memname=
);
%if not %length(&amp;amp;memname) %then %let memname=%scan(&amp;amp;infile,-2,'./"\');
%if not %length(&amp;amp;outfile) %then %let outfile=&amp;amp;memname..csv;
filename intrans "&amp;amp;infile";
proc cimport infile=intrans library=&amp;amp;libref;
run;
proc export data=&amp;amp;libref..&amp;amp;memname file="&amp;amp;outdir/&amp;amp;outfile" dbms=csv; 
run;
%mend convert_one;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then your program to actually convert all of the files can just be generated from the list of files.&amp;nbsp; For example by using a data step and CALL EXECUTE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname sasfiles 'C:\Users\Joaquin\Desktop';
%let outdir=C:\Users\Joaquin\Desktop\;

data _null_;
  set filelist ;
  call execute(cats('%nrstr(%convert_one)('
  ,'infile=',filename
  ,'outdir=',"&amp;amp;outdir"
 &amp;nbsp;,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Dec 2019 23:07:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Loop-over-files-for-cimport-and-export/m-p/613460#M18419</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-22T23:07:21Z</dc:date>
    </item>
  </channel>
</rss>

