BookmarkSubscribeRSS Feed
vipul257
Calcite | Level 5

Hi All,

 

I am trying to use the INFILE process to read a csv file.

 

However, the CSV file name contains a date (YYYYMMDD format) followed by a string which I need to capture as additional columns in the dataset that is created by the INFILE statement.

 

The resulting dataset I am looking for is:

Columns A - X: contents of the CSV file

Column Y: Date from the filename

Column Z: Part of a string from the filename.

 

it would be great if I can read multiple CSV files at once and read the date and string from each file name and append to the columns in the dataset.

 

Thanks for your help.

 

My code to simple read the file is:

 

DATA WORK.ALL1;

    LENGTH

        SOURCE_Name_1    $ 10

        SOURCE_Name_2    $ 50;

    FORMAT

        SOURCE_Name_1    $CHAR10.

        SOURCE_Name_2    $CHAR50.;

    INFORMAT

        SOURCE_Name_1    $CHAR10.

        SOURCE_Name_2    $CHAR50.;

    INFILE '\\filepath\*.csv'

           LRECL=32767

           DLM=','

          DSD

           FIRSTOBS=2

           MISSOVER;

    INPUT

        SOURCE_Name_1    : $CHAR10.

        SOURCE_Name_2    : $CHAR50.;

RUN;

 

2 REPLIES 2
Shmuel
Garnet | Level 18

Use the FILENAME= option in the INFILE statement to get the current csv input file

 

length csv_name _filepath $200;
infile '//filepath/*.csv'   filename = _filepath ... ;

csv_name = _filename;

then extract the date and required column using SUBSTR function.

Kurt_Bremser
Super User

Use the filename= option in the infile statement:

data work.all1;
length infilename $200;
infile "&filepath.\*.csv"
  lrecl=32767
  dlm=','
  dsd
  firstobs=2
  truncover
  filename=infilename
;

Now the variable infilename will hold the name of the current infile.

Be aware that the firstobs= option works only on the first file, header lines in the succeeding files need to be filtered in the step.

 

Edit: corrected filevar= to filename=

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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