Help using Base SAS procedures

Load .txt file and reclen

Reply
Contributor
Posts: 48

Load .txt file and reclen

I'm trying to load a .txt file into SAS, and have created two separate load files. One that looks at variable1A and the other, variable 1B. However, both variable1A and variable1B are in the same .txt file. The only way to separate the two is that variable 1A has a shorter record length than variable 1B. Is there a way to load the file so that only those records with variable1A (with say a maximum record length of 200) are in one SAS dataset, and then those variables with a reclen that exceed 200 are in another SAS dataset? 

Trusted Advisor
Posts: 2,113

Re: Load .txt file and reclen

Yes.  Use the LENGTH option in the INFILE statement.  Example 4 in the reference documentation may be helpful

SAS(R) 9.3 Statements: Reference

In general, something like this might work

FILENAME indata 'mydata.txt' LENGTH=linelen;

DATA A B;

INFILE indata;

INPUT <whatever>;

IF linelen=<onevalue> then output a;

ELSE IF linelen=<othervalue> THEN OUTPUT b;

ELSE <put error message>;

RUN;

PROC Star
Posts: 7,357

Re: Load .txt file and reclen

Without example data it is difficult to provide a working suggestion.  You can use the infile option "length=" to specify a variable, in your infile statement, that will capture each record's length (e.g., infile "c:\mydata.txt" length=reclen).

That way you can create both files in one pass.  e.g., pseudo code:

data long short;

  infile "c:\mydata.txt" length=reclen;

  input variable $varying500.;

  if reclen gt 200 then output long;

  else output short;

run;

Occasional Contributor
Posts: 14

Re: Load .txt file and reclen

Ashley,  it would be helpful to know how you are reading in the text file.  Can you do a simple length statement on both 1A and 1B then compare the lengths?

data short(keep=1A)

        long(keep=1B);

   set beginning;

   a=length(1A);

   b=length(1B);

  if (a < b) or (a<200) then output short;

  if (b > a) or (b>200) then output long;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 245 views
  • 0 likes
  • 4 in conversation