BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

Hello,

I want use data step to read a very large raw data with infile statement.

firstly I want to know how many rows in the file,if I only want to read the last records, I can use firstobs=n obs=n option but I need to know the value of n first.

Please help.

 

  DATA read_last_obs;
  INFILE raw1  FIRSTOBS=9999999 OBS=9999999.;
  INPUT a $50.;
  RUN;

 

Thanks!

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

I don't think so, reading in a text file is a linear process, its starts at character 1 and runs to the end of the file.  Why can you not proces the data once its read in, even really big files shouldn't take that long?  Why would you want only a few observations from the end?

LinusH
Tourmaline | Level 20

Take a look at the END= INFILE option. 

Data never sleeps
Astounding
PROC Star

If you are working under Linux/Unix this can be done.  But the exact statements are beyond my Unix knowledge.  Here's the idea.

 

Unix contains a "tail" command that lists end of a file.  Instead of listing it, the results can be piped to a file.

 

Now combine all of this with an INFILE statement.  The INFILE statements contains the "tail" command, piping its results as part of the INFILE statement definition (rather than to a file).  The combination lets the INFILE statement retrieve the tail end of the data source.

 

If you indicate that this would be useful for you, I'm sure someone on the board can give you more specific code.

GeorgeSAS
Lapis Lazuli | Level 10

I just want to get last row of record, this will save time and space if i know the total number  of rows

 

Thanks!

Astounding
PROC Star

Found a Unix command to count number of lines in a file:

 

wc -l filename

 

Are you working on Unix?

GeorgeSAS
Lapis Lazuli | Level 10

working on z/os

 

Thanks

Ksharp
Super User
 DATA _null_;
  INFILE '/folders/myfolders/all_jd.csv' end=last;
  INPUT;
  n+1;
  if last then putlog 'NOTE: File have ' n ' rows.'/
                      'The last row is:' _infile_ ;
  RUN;
GeorgeSAS
Lapis Lazuli | Level 10
Thank you Ksharp,
But your method need to read the whole raw file first, then counting the obs, while I don't want to read the whole file because time cost a lot!
Thanks!
Ksharp
Super User
Maybe you should take a look at the function relate to FILE,
Like FGET(), FOPEN() .............

But I totally don't have any clue about it .

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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