BookmarkSubscribeRSS Feed
shellp55
Quartz | Level 8
Hello

I am using SAS 9.1.

I have mulitple .txt files and I'm using the infile and input statements to extract the data:

filename stuff('c:\mydirectory\fun1.txt' 'c:\mydirectory\fun2.txt' 'c:\mydirectory\fun3.txt)

data mylib.test
infile stuff;

input @1 ID $1.
@2 Name $15
etc.

But what if the data elements were the same but located in different areas of the file from one another, is it possible to still have all in the same program i.e. if 'c:\mydirectory\fun1.txt then
input @1 ID $1
@5 Name $15;


else if 'c:\mydirectory\fun2.txt then
input @1 ID $1
@ 2 Name $15


If not like above then how to do? Thanks.
5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Using a DATA step approach, use an INPUT statement as shown below to load the input buffer but without reading any variables and use the special SAS variable _INFILE_ to detect what input file format you are expected, then follow that SAS INPUT statement with one or more as determined by the record-format:
DATA ....;
* START OF YOUR DATA STEP CODE APPEARS HERE. ;
INPUT @;
IF _INFILE_ =: 'FORMAT1' THEN DO;
END;
ELSE IF _INFILE_ =: 'FORMAT2' THEN DO;
END;
ELSE DO;
* SHOULD NOT MAKE IT HERE. ;
ABORT ;
END;
* MORE OF YOUR DATA STEP CODE APPEARS HERE. ;
RUN;

Scott Barry
SBBWorks, Inc.
shellp55
Quartz | Level 8
HI Scott

Sorry but I'm still a newbie at this so I'm confused by your answer.

Are you saying that I need to create a different set of inputs for every file or just the ones that are different?

Where does "format1" and "format2" come from (using my example)?

Thanks.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Only one per each unique format - that being some characteristic of your data structure that appears to be unique, which would be detected by some SAS INPUT statement logic to read a part of the record that appears to be unique. This logic would determine what code path to take.

Scott Barry
SBBWorks, Inc.
shellp55
Quartz | Level 8
Hi

I'm not sure I'm explaining properly: I want the contents of multiple text files to be imported into a single SAS data set.

All 4 files to import have the same data elements, just located in different sections of the file i.e. postal code is input @72 but for another file postal code is found at location @87.

So there isn't anything unique with the data structure itself, it's the file name and location. There also isn't any "code path to take" other than what location to identify for the input statement.

So if you still think what you're telling me will work can you please provide the code structure using actual code because I don't understand what you're saying? Thanks.
Cynthia_sas
Diamond | Level 26
Hi:
If you look in the documentation on this page:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000146932.htm
about the INFILE statement, you will find reference to this topic:
"Example 5: Reading from Multiple Input Files" from here
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000146932.htm#a000177201

There you will find 2 different ways to read from multiple input files. The second program under Example 5 uses the FILEVAR method.

cynthia

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

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 1488 views
  • 0 likes
  • 3 in conversation