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

I am trying to read in data from all of the files from a particular directory.  My variable "allpath" specifies the location of each file.  I am starting to read each file at observation 18.  However, some files do not contain at least 18 lines.  What happens is that upon the first occurrence of a file with <18 lines, I receive a "NOTE: LOST CARD." statement and an error message, then the file read-in process stops.  I have all of the data read in up to that point output to jtest, but nothing beyond that.  I was on the verge of hardcoding to delete those records from "allpath", but I prefer to handle it automatically by having SAS either ignore those files, given the inadequate number of data lines or export a row of missing variables for those files.

 

QUESTION: How do I keep reading in additional files after having a file without data?

 

Thanks for your help!

shorine

 

Here is my code (dataset jabc1 contains all of the pathing to the files):

 

data jtest;
set jabc1;
format flagjet 3.;
do until(done);
  infile indummy filevar=allpath firstobs=18 obs=317 end=done;

    input @1 anyrec $4.;
    if done then flagjet=1;
    output;
end;
run;

 

Here is the error message from my log (there are approx 65 files to read in, so not all are being processed):


NOTE: LOST CARD.
subjid=1234 visit=1 task=ABC
allpath=D:\MYFOLDER\ABC_Subj1234_Visit1_Rep1.txt

flagjet=. done=1 anyrec= _ERROR_=1 _N_=27
NOTE: There were 27 observations read from the data set WORK.JABC1.
NOTE: The data set WORK.JTEST has 7204 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds

 

An inadequate dataset looks like:

*************************************************************
2018-04-10 10:18:44 AM
Subject:1234
Visit:1

 

 

A good dataset looks like:

*************************************************************
2017-08-17 12:23:50 PM
Subject:4321
Visit:1

Criterion1: zz
Criterion2; d
Criterion3: w
-
-
-
-
-
-
-
-
-
-
1 d
2 f
3 d
4 c
5 e

... up to 300 records

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Make sure to test the END= variable so that you do not read past the end of any file. Also add the TRUNCOVER option so that trying to read past the end of an individual line does not cause the INPUT statement to try to read another line.

data jtest;
  set jabc1;
  infile indummy filevar=allpath truncover obs=317 end=done;
  do _N_=1 to 17 while (not done);
    input; 
  end;
  do while(not done);
    input anyrec $4.;
    flagjet=done ;
    output;
  end;
run;

 

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

Like this?

  infile INDUMMY filevar=ALLPATH obs=317 end=DONE pad;
  do until(DONE or RECNO=18);
    input @1 ANYREC : $4.;       
    RECNO+1;
  end;
  if RECNO=18 then do until(DONE);
    output;
    if DONE then FLAGJET=1;
    else input @1 ANYREC : $4.;
  end;

 

shorine
Calcite | Level 5
I think that this code may work with some small tweaks, but I was not able to get it to work properly.
Tom
Super User Tom
Super User

Make sure to test the END= variable so that you do not read past the end of any file. Also add the TRUNCOVER option so that trying to read past the end of an individual line does not cause the INPUT statement to try to read another line.

data jtest;
  set jabc1;
  infile indummy filevar=allpath truncover obs=317 end=done;
  do _N_=1 to 17 while (not done);
    input; 
  end;
  do while(not done);
    input anyrec $4.;
    flagjet=done ;
    output;
  end;
run;

 
shorine
Calcite | Level 5
This code worked. Thanks very much! shorine

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