<?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: Reading files into sas using macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649063#M194545</link>
    <description>Thank you so much for the insight and detailed instructions! This worked like a charm and got rid of the messy macro! Thanks again.</description>
    <pubDate>Wed, 20 May 2020 00:23:43 GMT</pubDate>
    <dc:creator>bknitch</dc:creator>
    <dc:date>2020-05-20T00:23:43Z</dc:date>
    <item>
      <title>Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649032#M194521</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro read_files (out=zip_file);

%let  FOLDER = C:\planinfo\IRS_DATA;

filename _tree pipe "DIR /b &amp;amp;FOLDER *.txt ";

data temp1;

infile _tree pad recfm=v lrecl=1024;

length file path $200;

input file $ ;
path=cats("&amp;amp;folder","\",file);
if file = "WindowsCodecsRaw.txt" then delete;

run;

 

%Local i work_dsn n_ids;

 

   proc sql noprint;

   select

   count(*)

   into :n_ids

   from

   temp1;quit;

 %put n_ids=&amp;amp;n_ids;

 

   %Do i = 1 %To &amp;amp;n_ids;

      %Let last_n = %Eval(&amp;amp;i - 1);

         Proc Sql noprint;

         Select distinct path Into :filename Separated by ','

           From temp1 (FirstObs=&amp;amp;i obs=1)

                ;Quit;

%put filename=&amp;amp;filename;


proc import

datafile= "&amp;amp;filename"

out=temp2

dbms=csv

replace

;

delimiter=','

;getnames=yes ;

run;


%if &amp;amp;i=1 %then %do;

 

%if %sysfunc(exist(&amp;amp;out)) %then %do;

Proc Sql Noprint;

      Drop Table &amp;amp;out;

   Quit;

%end;

 

data &amp;amp;out;

set temp2;

run;

 

%end;

%else %do;


Proc Append Base=&amp;amp;out Data=temp2 force; Run;

%end;



Proc Sql Noprint;

      Drop Table work.temp2;

   Quit;

 

  %end;

 

  %mend;

 

 

%read_files;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm utilizing the code to import all txt files to append to the first read file. It's receiving an error on the firstobs. Its reading the path correctly but for some reason I can't figure out why its not recreating the other 2 files. It seems to be replicating the first file. Any help would be appreciated. Here is the error below and a screenshot of my TEMP1 dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: FIRSTOBS option &amp;gt; OBS option - no data to read from file WORK.TEMP1.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="bknitch_0-1589926514055.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39639iC824BE31796FEDA5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="bknitch_0-1589926514055.png" alt="bknitch_0-1589926514055.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 22:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649032#M194521</guid>
      <dc:creator>bknitch</dc:creator>
      <dc:date>2020-05-19T22:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649037#M194522</link>
      <description>&lt;P&gt;The OBS= option is more like a LASTOBS= option.&amp;nbsp; So it worked when you had FIRSTOBS=1 and OBS=1 since that means read from 1 to 1.&amp;nbsp; But then next time through the loop you asked for FIRSTOBS=2 and OBS=1 which looks like you are asking SAS to read the file backwards from 2 to 1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just make this change to fix that issue.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;From temp1 (FirstObs=&amp;amp;i obs=&amp;amp;i)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The bigger issue that you are reading 3 files with PROC IMPORT and then expecting the results to be compatible enough that you can use PROC APPEND to combine them.&amp;nbsp; That is not going to work because PROC IMPORT has to guess each time how to define the variables based solely on the examples it sees in this one version of the file.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you know what is in the files just write your own data step to read them.&amp;nbsp; Which also means you don't need the rest of that complicated macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let out=zip_file;
%let  FOLDER = C:\planinfo\IRS_DATA;

data &amp;amp;out;
  length filename $200; 
  infile "&amp;amp;folder\*.txt" dsd truncover filename=filename;
  input @;
  * Skip header row ;
  if filename ne lag(filename) then delete ;
  input var1 var2 ... .;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really want to let PROC IMPORT figure out want is in the files but you know they have the same columns in the same order then combine the files and let it work on the combined file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename combined temp;
data _null_;
  length filename $200; 
  infile "&amp;amp;folder\*.txt"  filename=filename;
  file combined ;
  input ;
  * Skip header row ;
  if filename ne lag(filename) and _n_ &amp;gt;1 then delete;
  put _infile_;
run;
proc import datafile=combined dbms=csv out=&amp;amp;out replace;
  getnames=yes;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;EM&gt;Update: FILENAME= not FILEVAR=&amp;nbsp;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2020 01:04:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649037#M194522</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-20T01:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649040#M194525</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp; Thank you so much for the insight. I tried using the data step option, received an error "ERROR: A Physical file reference (i.e. "PHYSICAL FILE REFERENCE" ) or an aggregate file storage reference (i.e. AGGREGATE(MEMBER) ) reference cannot be used with the FILEVAR= option."&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I saw a solution you suggested on a previous source, so i changed the FILEVAR option to Filename=Filename, i'm getting null results for all with no errors (i'll take the null errors &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; ) Not sure why my data is null. Any thoughts?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 23:07:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649040#M194525</guid>
      <dc:creator>bknitch</dc:creator>
      <dc:date>2020-05-19T23:07:03Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649043#M194528</link>
      <description>&lt;P&gt;Heres a snippet from the log&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: Invalid data for STATEFIPS in line 2 1-775.&lt;BR /&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0&lt;/P&gt;
&lt;P&gt;2 CHAR 1.AL.0.1.0_25.836320.481570.109790.233260.455560.1356760.525260.26020.16850.9170.5730.11620.255610.1&lt;BR /&gt;ZONE 3044030303533033333303333330333333033333303333330333333303333330333330333330333303333033333033333303&lt;BR /&gt;NUMR 191C909190F25983632094815709109790923326094555609135676095252609260209168509917095730911620925561091&lt;/P&gt;
&lt;P&gt;101 49910.10855656.836310.11036309.669770.8646476.92010.59467.43130.72032.38540.47871.12050.5426.146630.&lt;BR /&gt;ZONE 3333303333333303333330333333330333333033333330333330333330333330333330333330333330333330333303333330&lt;BR /&gt;NUMR 4991091085565698363109110363099669770986464769920109594679431309720329385409478719120509542691466309&lt;BR /&gt;2&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 23:15:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649043#M194528</guid>
      <dc:creator>bknitch</dc:creator>
      <dc:date>2020-05-19T23:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649046#M194531</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/192500"&gt;@bknitch&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Heres a snippet from the log&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: Invalid data for STATEFIPS in line 2 1-775.&lt;BR /&gt;RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0&lt;/P&gt;
&lt;P&gt;2 CHAR 1.AL.0.1.0_25.836320.481570.109790.233260.455560.1356760.525260.26020.16850.9170.5730.11620.255610.1&lt;BR /&gt;ZONE 3044030303533033333303333330333333033333303333330333333303333330333330333330333303333033333033333303&lt;BR /&gt;NUMR 191C909190F25983632094815709109790923326094555609135676095252609260209168509917095730911620925561091&lt;/P&gt;
&lt;P&gt;101 49910.10855656.836310.11036309.669770.8646476.92010.59467.43130.72032.38540.47871.12050.5426.146630.&lt;BR /&gt;ZONE 3333303333333303333330333333330333333033333330333330333330333330333330333330333330333330333303333330&lt;BR /&gt;NUMR 4991091085565698363109110363099669770986464769920109594679431309720329385409478719120509542691466309&lt;BR /&gt;2&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Start with the DATA statement through the RUN with all the notes. Paste the LOG into a code box opened with the &amp;lt;/&amp;gt; icon on the form to preserve formatting.&lt;/P&gt;
&lt;P&gt;The error indicates that what ever the data may be the INPUT information you gave it is incompatible with the data for that line (and likely others).&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 23:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649046#M194531</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-19T23:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649047#M194532</link>
      <description>Sounds like you tried to just read the directory instead of actually trying to use the wildcard to name a file. You might want to add %str() around the * to prevent SAS from seeing /* as starting a comment.  Either that or you have a sub directory named whose name ends in .txt.&lt;BR /&gt;</description>
      <pubDate>Tue, 19 May 2020 23:19:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649047#M194532</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-19T23:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649048#M194533</link>
      <description>So your other code wasn't going to work either.  That file is using TAB (09 in hexcode) as the delimiter, not comma.&lt;BR /&gt;</description>
      <pubDate>Tue, 19 May 2020 23:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649048#M194533</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-19T23:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: Reading files into sas using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649063#M194545</link>
      <description>Thank you so much for the insight and detailed instructions! This worked like a charm and got rid of the messy macro! Thanks again.</description>
      <pubDate>Wed, 20 May 2020 00:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reading-files-into-sas-using-macro/m-p/649063#M194545</guid>
      <dc:creator>bknitch</dc:creator>
      <dc:date>2020-05-20T00:23:43Z</dc:date>
    </item>
  </channel>
</rss>

