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
SAS Super FREQ
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

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