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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 17 replies
  • 3631 views
  • 0 likes
  • 6 in conversation