DATA Step, Macro, Functions and more

Read the last record of csv file in another dataset

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

Read the last record of csv file in another dataset

[ Edited ]

Hello,

 

I have the attached raw file on a unix server.

 

Alfred|14|M|69|112.5
Alice|13|F|56.5|84
Barbara|13|F|65.3|98
Carol|14|F|62.8|102.5
.(MORE RECORDS).

Thomas|11|M|57.5|85
William|15|M|66.5|112
87654321

 

I have to read the file completely but the last record is the observation count of the file given by source.

I have to read this LAST number in another file.

 

How should i do that?Attached is a test file for reference.

 

Here is what i need: 2 datasets.

1 with regular input of all variables.

and other with just one obervation and one variable having the last record of count i.e.--"87654321"

 

Thanks


Accepted Solutions
Solution
‎09-26-2017 03:56 PM
Super User
Super User
Posts: 8,289

Re: Read the last record of csv file in another dataset

[ Edited ]
Posted in reply to ArpitSharma

Do something like this perhaps?

filename tst temp;
data _null_;
  file tst;
  input;
  put _infile_;
cards;
Alfred|14|M|69|112.5
Alice|13|F|56.5|84
Barbara|13|F|65.3|98
Carol|14|F|62.8|102.5
Thomas|11|M|57.5|85
William|15|M|66.5|112
6
;

data want(drop=nobs) nobs(keep=nobs);
  infile tst dsd dlm='|' truncover end=eof ;
  input @;
  if eof then do;
    input nobs;
    output nobs;
    if nobs ne _n_-1 then put
      'WARNING: Record count missmatch. ' (_n_ nobs) (= :comma20. +1)
    ;
  end;
  else do;
    length name $20 age 8 sex $1 height weight 8 ;
    input name -- weight ;
    output want ;
  end;
run;
 
 NOTE: 7 records were read from the infile TST.
       The minimum record length was 80.
       The maximum record length was 80.
 NOTE: The data set WORK.WANT has 6 observations and 5 variables.
 NOTE: The data set WORK.NOBS has 1 observations and 1 variables.

 

View solution in original post


All Replies
Super User
Posts: 6,939

Re: Read the last record of csv file in another dataset

Posted in reply to ArpitSharma

It's not difficult, but it's not clear either.  Try taking the small amount of data shown in your post, and sketch out what you would like the result to be.  Be sure to indicate what is a SAS data set and what is a text file.

Respected Advisor
Posts: 3,294

Re: Read the last record of csv file in another dataset

Posted in reply to ArpitSharma

If you can read this file properly including the last row ... then just modify the code

 

Something like:

 

data one two;
     infile filename end=eof;
     .... statements to read data ....
     if not eof then output one;
     else output two;
run;
--
Paige Miller
Super User
Posts: 10,623

Re: Read the last record of csv file in another dataset

[ Edited ]
Posted in reply to ArpitSharma

To expand on @PaigeMiller's proposal, I'd do

data
  one (drop=reccount)
  two (keep=reccount)
,
infile "xxx.txt" end=eof;
if eof
then do;
  input reccount;
  output two;
end;
else do;
  input /* your variables */;
  output one;
end;
run;

This also solves the problem of different line layouts.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎09-26-2017 03:56 PM
Super User
Super User
Posts: 8,289

Re: Read the last record of csv file in another dataset

[ Edited ]
Posted in reply to ArpitSharma

Do something like this perhaps?

filename tst temp;
data _null_;
  file tst;
  input;
  put _infile_;
cards;
Alfred|14|M|69|112.5
Alice|13|F|56.5|84
Barbara|13|F|65.3|98
Carol|14|F|62.8|102.5
Thomas|11|M|57.5|85
William|15|M|66.5|112
6
;

data want(drop=nobs) nobs(keep=nobs);
  infile tst dsd dlm='|' truncover end=eof ;
  input @;
  if eof then do;
    input nobs;
    output nobs;
    if nobs ne _n_-1 then put
      'WARNING: Record count missmatch. ' (_n_ nobs) (= :comma20. +1)
    ;
  end;
  else do;
    length name $20 age 8 sex $1 height weight 8 ;
    input name -- weight ;
    output want ;
  end;
run;
 
 NOTE: 7 records were read from the infile TST.
       The minimum record length was 80.
       The maximum record length was 80.
 NOTE: The data set WORK.WANT has 6 observations and 5 variables.
 NOTE: The data set WORK.NOBS has 1 observations and 1 variables.

 

Contributor
Posts: 38

Re: Read the last record of csv file in another dataset

The

input @;

is the key.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 261 views
  • 3 likes
  • 5 in conversation