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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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