DATA Step, Macro, Functions and more

how many records in the raw data

Reply
Regular Contributor
Posts: 241

how many records in the raw data

[ Edited ]

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!

Super User
Super User
Posts: 7,942

Re: how many records in the raw data

Posted in reply to GeorgeSAS

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?

Super User
Posts: 5,426

Re: how many records in the raw data

Posted in reply to GeorgeSAS

Take a look at the END= INFILE option. 

Data never sleeps
Super User
Posts: 5,498

Re: how many records in the raw data

Posted in reply to GeorgeSAS

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.

Regular Contributor
Posts: 241

Re: how many records in the raw data

Posted in reply to Astounding

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

 

Thanks!

Super User
Posts: 5,498

Re: how many records in the raw data

Posted in reply to GeorgeSAS

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

 

wc -l filename

 

Are you working on Unix?

Regular Contributor
Posts: 241

Re: how many records in the raw data

Posted in reply to Astounding

working on z/os

 

Thanks

Super User
Posts: 10,023

Re: how many records in the raw data

Posted in reply to GeorgeSAS
 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;
Regular Contributor
Posts: 241

Re: how many records in the raw data

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!
Super User
Posts: 10,023

Re: how many records in the raw data

Posted in reply to GeorgeSAS
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 .

Ask a Question
Discussion stats
  • 9 replies
  • 342 views
  • 0 likes
  • 5 in conversation