BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ArpitSharma
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Astounding
PROC Star

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.

PaigeMiller
Diamond | Level 26

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
Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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.

 

ArpitSharma
Fluorite | Level 6

The

input @;

is the key.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 1783 views
  • 3 likes
  • 5 in conversation