BookmarkSubscribeRSS Feed
AshleyM
Fluorite | Level 6

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? 

3 REPLIES 3
Doc_Duke
Rhodochrosite | Level 12

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;

art297
Opal | Level 21

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;

bwasicak
Calcite | Level 5

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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