Help using Base SAS procedures

Import a text file with a date in the name

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 114
Accepted Solution

Import a text file with a date in the name

Hi I have a text file coming in with a date
Report 2018-04-01.csv
Since that file date will change base on when report is run
I need to import today date file. Report 2018-04-02.csv
I was trying a proc import out= report
Datafile ="h:\ report &sysdate.csv"
Dbms= Csv replace;
Getnames=yes;
Datarows=2;
Run;

Accepted Solutions
Solution
‎04-05-2018 11:16 AM
Super User
Posts: 13,583

Re: Import a text file with a date in the name

I would be very leery of letting an automated process with Proc Import read my data. As a minimum you should add the guessingrows=max option to have the best chance of consistent data.

Proc Import has to guess at lengths and types of variables and depending on the actual contents the types and lengths may change between files read.

I would suggest reading one file with import, go to the log and extract the data step generated and edit that code. Use the same code to read all of the files just changing input file names and output data set names.

 

If you go to combine data and get warnings or errors about mismatched data types or truncated character variables don't say you haven't been warned.

View solution in original post


All Replies
Super User
Super User
Posts: 8,120

Re: Import a text file with a date in the name

You should be able to create a macro variable with today's date in that format.

%let today=%sysfunc(today(),yymmddd10);

And then use the macro variable in the filename.

Datafile ="h:\report &today..csv"

Make sure to have a "extra" period. The first one will be used by macro processor to indicate the end of the macro variable name and the second one will become part of the generated filename.

You could just put the macro function call directly into the string, that will eliminate the need for the macro variable and the extra period.

Datafile ="h:\report %sysfunc(today(),yymmddd10).csv"
Solution
‎04-05-2018 11:16 AM
Super User
Posts: 13,583

Re: Import a text file with a date in the name

I would be very leery of letting an automated process with Proc Import read my data. As a minimum you should add the guessingrows=max option to have the best chance of consistent data.

Proc Import has to guess at lengths and types of variables and depending on the actual contents the types and lengths may change between files read.

I would suggest reading one file with import, go to the log and extract the data step generated and edit that code. Use the same code to read all of the files just changing input file names and output data set names.

 

If you go to combine data and get warnings or errors about mismatched data types or truncated character variables don't say you haven't been warned.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 158 views
  • 0 likes
  • 3 in conversation