Help using Base SAS procedures

Different fields to Import per file

Reply
Regular Contributor
Posts: 152

Different fields to Import per file

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.
Super Contributor
Super Contributor
Posts: 3,174

Re: Different fields to Import per file

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.
Regular Contributor
Posts: 152

Re: Different fields to Import per file

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.
Super Contributor
Super Contributor
Posts: 3,174

Re: Different fields to Import per file

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.
Regular Contributor
Posts: 152

Re: Different fields to Import per file

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.
SAS Super FREQ
Posts: 8,744

Re: Different fields to Import per file

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
Ask a Question
Discussion stats
  • 5 replies
  • 100 views
  • 0 likes
  • 3 in conversation