BookmarkSubscribeRSS Feed
WendyT
Pyrite | Level 9
I have a similar situation. The structure of the files is the same, but the file names are random, and over time, more files are added.

This program uses the x command to go to Windows and generate a file with the names of all *.csv files in a directory. Then control comes back to SAS, and the INFILE statement uses the generated file with the FILEVAR option to read and stack the files.

All you need to know to use this method is the directory and filetype.

I left the length and input statements as is so you could see the structure.

Wendy


filename par_files 'D:\yada\my_files.sas' ;
x 'cd D:\yada\my_directory' ;
x 'del my_files.sas' ;
x 'dir *.csv /a-d /B /L > my_files.sas' ;
x 'exit' ;

DATA PAR_FILES ;

LENGTH
READFILE $ 200
STATION $ 20
PAR1_ $ 10
PAR2_ $ 10
PAR3_ $ 10 ;

FORMAT SASDATE YYMMDD6. ;

INFILE PAR_FILES ;
INPUT READFILE $ ;
INFILE FILE FILEVAR=READFILE END=DONE MISSOVER PAD LRECL=900 FIRSTOBS=4 DLM=',' ;
DO UNTIL (DONE) ;

INPUT
STATION $ SASDATE YYMMDD8. TIME1 REP SET DPTHSTR PAR_K
DPTHCOL1 PAR1 PAR1_ $
DPTHCOL2 PAR2 PAR2_ $
DPTHCOL3 PAR3 PAR3_ $ ;

OUTPUT ;
END ;

RUN ;
deleted_user
Not applicable
SASuser01

because you have SAS you have a simple process to load this data.
It is a data step.
The infile statement (except on ZOS mainframes, which I think is NOT your platform) allow you to define the physical file names with an *, like[pre] infile '\\path\hclr*.txt' ; [/pre]That takes care of all that macro looping. You need only one infile statement. (keep files with similar names that you do not wish to load, in a different folder).
What remains is the code to read your columns. As your files all have the same layout, you need only one set of code to read them.
Since you don't describe your text file in detail, I won't offer code to read it.

good Luck

PeterC
WendyT
Pyrite | Level 9
PeterC-

That's a MUCH better solution than mine - I'll give it a try.

Thanks for the help!

Wendy

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 17 replies
  • 4285 views
  • 0 likes
  • 6 in conversation