BookmarkSubscribeRSS Feed
SASGeek
Obsidian | Level 7

Hello all,

I'm pulling in several text files (up to 100) stored on our SAS server that has a date on the file name that I need to incorporate into the imported file itself.

 

For example: From the infile statement below I need to extract the 20230221 portion of the file name and insert it as a variable into the file I'm creating. There is no rhyme or reason for the date sequence, just the date it's run on.

If I knew how to extract that date I could put it into a do-loop and run it that way. But the question is: how do I extract that file the file name for 100 text files?

 

Thanks!

 

INFILE '/sasdata/US/Sales/Weekly_Sales_20230221.txt'
LRECL=32767
FIRSTOBS=2
ENCODING="LATIN1"
DLM='09'x
MISSOVER
DSD ;

 

2 REPLIES 2
SASKiwi
PROC Star

How are you creating the INFILE statement in the first place? Is it maintained manually or is it generated automatically? In any case just add the FILENAME option on your INFILE statement and the variable you specify will contain the name of the file you are reading:

INFILE '/sasdata/US/Sales/Weekly_Sales_20230221.txt'
FILENAME = MyFile
LRECL=32767
FIRSTOBS=2
ENCODING="LATIN1"
DLM='09'x
MISSOVER
DSD ;

 

Kurt_Bremser
Super User

Two things to keep in mind:

  • the variable created by the FILENAME= option is temporary and not included in the dataset; you need to create another one for storage in the dataset
  • define the length of the variable(s) before the INFILE statement, so they don't default to $8

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 512 views
  • 0 likes
  • 3 in conversation