DATA Step, Macro, Functions and more

How to write a report if infile is empty

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

How to write a report if infile is empty

Hi ,

I have the below portion of my  code :

 

 IF CURR  > DATE_FROM_FORMAT THEN DO ;                    
      INFILE FILENMS DLM ='~' ;                           
       LENGTH    FILE2RD  $45. ;                          
       INPUT  @1 FILE2RD  $;                              
                 FILE2RD = TRIM(FILE2RD) ;                
       INFILE DUMMY DLM='~' FILEVAR=FILE2RD DSD MISSOVER ;
         IF _N_ = 0 THEN DO ;                             
         PUT 'FILE2RD IS EMPTY' ;                         
         END;                                             
END ;      

 

But the output shown in saslog is :

 

NOTE: The infile FILENMS is:                                          
      Dsname=CHMICST.X.MACRO.SAYAN,                                   
      Unit=3390,Volume=BP3043,Disp=SHR,Blksize=27998,                 
      Lrecl=600,Recfm=VB                                              
                                                                      
   SAYAN1:CURR=27                                                     
   SAYAN2Smiley Very HappyATE_FROM_FORMAT=15                                         
NOTE: The infile DUMMY is:                                            
      Dsname=CHMICST.MPB.INPUT.TSV.SAYAN,                             
      Unit=3390,Volume=BL3186,Disp=SHR,Blksize=4500,                  
      Lrecl=1500,Recfm=VB                                             
                                                                      
NOTE: 1 record was read from the infile FILENMS.                      
      The minimum record length was 71.                               
      The maximum record length was 71.                               
NOTE: 0 records were read from the infile DUMMY.                      

 

I want to generate a report if the input file 'DUMMY' is having no records. How can I do that ?                                               


Accepted Solutions
Solution
‎07-28-2017 06:27 AM
Respected Advisor
Posts: 3,799

Re: How to write a report if infile is empty

Posted in reply to sayanapex06

You need to use INFILE statement option END= and check it before you try to read any records.  For example.

 

28         filename FT15F001 temp;
29         parmcards;

30         ;;;;
31            run;
32         data _null_;
33            filevar = pathname('FT15F001','F');
34            infile dummy filevar=filevar END=EOF;
35            if eof then do;
36               putlog 'NOTE: EOF on ' Filevar= ;
37               end;
38            do while(not eof);
39               input;
40               putlog _infile_;
41               end;
42            stop;
43            run;

NOTE: The infile DUMMY is:
      (system-specific pathname), 
      (system-specific file attributes)

NOTE: EOF on filevar=system-specific pathname
NOTE: 0 records were read from the infile (system-specific pathname).
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: How to write a report if infile is empty

Posted in reply to sayanapex06
data _null_;
  set sashelp.table (where=(libname="<your lib>" and memname="<your data>" and nobs=0));
  call execute('proc report <do rest of report>; run;');
run;

With the above, from the metadata only 1 row will be returned if the dataset has zero obs, that way the call execute will only be executed if the obs=0, otherwise it will not do anything.  Note <your lib> is the library name in caps, <your data> is your dataset name in caps. And <do rest of report> needs to be replaced with the proc report information.

Contributor
Posts: 37

Re: How to write a report if infile is empty

Thanks, but that is not what I asked for

Super User
Posts: 19,772

Re: How to write a report if infile is empty

Posted in reply to sayanapex06

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p011imau3tm4jen1u...

See the example above that shows how to print a message if the dataset is empty

Contributor
Posts: 37

Re: How to write a report if infile is empty

Reeza I specifically mentioned infile not a sas dataset,.. i am reading the infile dummy from filevar.

 

Please help me to write a report if the dummy file is empty.

 

*****not a sas dataset*****

Trusted Advisor
Posts: 1,554

Re: How to write a report if infile is empty

[ Edited ]
Posted in reply to sayanapex06

Try next code:

 

 IF CURR  > DATE_FROM_FORMAT THEN DO ;                    
       INFILE FILENMS DLM ='~' ;                           
       LENGTH    FILE2RD  $45. ;                          
       INPUT  @1 FILE2RD  $;                              
                 FILE2RD = TRIM(FILE2RD) ;                
       INFILE DUMMY DLM='~' FILEVAR=FILE2RD DSD MISSOVER eov=done ;
retain m 0; input; IF done and m= 0 THEN DO ; PUT 'FILE2RD IS EMPTY' ; END; m+1; END ;
Contributor
Posts: 37

Re: How to write a report if infile is empty

Hi Shmuel,

Thanks for the next portion

 

but this didnt work :

 

   IF CURR  > DATE_FROM_FORMAT THEN DO ;                             
        INFILE FILENMS DLM ='~' ;                                    
         LENGTH    FILE2RD  $45. ;                                   
         INPUT  @1 FILE2RD  $;                                       
                   FILE2RD = TRIM(FILE2RD) ;                         
         INFILE DUMMY DLM='~' FILEVAR=FILE2RD DSD MISSOVER EOV=DONE ;
          RETAIN M 0;                                                
          INPUT ;                                                    
           IF DONE AND M=0 THEN DO ;                                 
           PUT 'FILE2RD IS EMPTY' ;                                  
           END;                                                      
           M+1 ;                                                     
  END ;                                                              

Super User
Posts: 19,772

Re: How to write a report if infile is empty

Posted in reply to sayanapex06

Post the full code and log.

Contributor
Posts: 37

Re: How to write a report if infile is empty

//CHXXROCK JOB EI4605,                             
//         'S.DIONNE.BLDG-610',                    
//          CLASS=S,                               
//          MSGCLASS=X,                            
//          MSGLEVEL=(1,1),                        
//          NOTIFY=&SYSUID,                        
//          RD=NC,                                 
//          RESTART=*                              
//*                                                
//TEST1    EXEC SAS                                
//FILENMS  DD DISP=SHR,DSN=CHMICST.X.MACRO.SAYAN   
//SASLIB DD DSN=CHMICST.SAS.CODE.NEWDEV,DISP=SHR   
//EMAIL  DD SYSOUT=*                               
//SYSOUT   DD SYSOUT=*                             
//SYSPRINT DD SYSOUT=*                             
//SYSIN DD *                                       
OPTIONS SOURCE SOURCE2 NOCENTER;                   
   DATA _NULL_ ;                                                
   %INCLUDE SASLIB(TEST) ;                                     
  RUN ;                                                        
                                                               
 DATA MACRO_FILES ;                                            
   LENGTH   BLUE_FILE_NAME                                     
            RESPONSIBLE_PERSON  $200.                          
            ;                                                  
   INFILE FILENMS DELIMITER='~' DSD MISSOVER END=LASTOBS;      
   INPUT @1 BLUE_FILE_NAME                                     
            RESPONSIBLE_PERSON                                 
            ;                                                  
   BLUE_FILE_NAME         = UPCASE(BLUE_FILE_NAME);            
   RESPONSIBLE_PERSON     = UPCASE(RESPONSIBLE_PERSON);        
  IF BLUE_FILE_NAME     = 'BLUE_FILE_NAME' THEN DELETE ;       
  IF RESPONSIBLE_PERSON = 'RESPONSIBLE_PERSON' THEN DELETE ;   
                                                               
 DATA EMPTY_FILES ;                                            

    SET MACRO_FILES;                                                  
    KEEP BLUE_FILE_NAME RESPONSIBLE_PERSON ;                         
         DATE_FROM_FORMAT = INPUT(BLUE_FILE_NAME,TEST.) ;            
         TODAY=COMPRESS(PUT(TODAY(),MMDDYYN7.));                     
         CURR=SUBSTR(TODAY,3,2);                                     
     PUT @4 'SAYAN1:' CURR= ;                                        
     PUT @4 'SAYAN2:' DATE_FROM_FORMAT=;                             
   IF CURR  > DATE_FROM_FORMAT THEN DO ;                             
        INFILE FILENMS DLM ='~' ;                                    
         LENGTH    FILE2RD  $45. ;                                   
         INPUT  @1 FILE2RD  $;                                       
                   FILE2RD = TRIM(FILE2RD) ;                         
         INFILE DUMMY DLM='~' FILEVAR=FILE2RD DSD MISSOVER EOV=DONE ;
          RETAIN M 0;                                                
          INPUT ;                                                    
           IF DONE AND M=0 THEN DO ;                                 
           PUT 'FILE2RD IS EMPTY' ;                                  
           END;                                                      

           M+1 ;                               
  END ;                 

 

file : CHMICST.X.MACRO.SAYAN :  CHMICST.MPB.INPUT.TSV.SAYAN~Sayan.Chakraborty1@ge.com     

saslib : 'CHMICST.SAS.CODE.NEWDEV(test)'  : 

PROC FORMAT;                                 
INVALUE TEST                                 
'CHMICST.MPB.INPUT.TSV.SAYAN' = 15           
;        

 

SASLOG :

 

NOTE: The infile FILENMS is:                                                   
      Dsname=CHMICST.X.MACRO.SAYAN,                                            
      Unit=3390,Volume=BP3043,Disp=SHR,Blksize=27998,                          
      Lrecl=600,Recfm=VB                                                       
                                                                               
NOTE: 1 record was read from the infile FILENMS.                               
      The minimum record length was 71.                                        
      The maximum record length was 71.                                        
NOTE: The data set WORK.MACRO_FILES has 1 observations and 2 variables.        
NOTE: The DATA statement used 0.01 CPU seconds and 14461K.                     
                                                                               
NOTE: The address space has used a maximum of 632K below the line and 15592K above the line.  

 

 NOTE: Character values have been converted to numeric values at the places give
       32:6                                                                    
 NOTE: The infile FILENMS is:                                                  
       Dsname=CHMICST.X.MACRO.SAYAN,                                           
       Unit=3390,Volume=BP3043,Disp=SHR,Blksize=27998,                         
       Lrecl=600,Recfm=VB                                                      
                                                                               
    SAYAN1:CURR=27                                                             
    SAYAN2Smiley Very HappyATE_FROM_FORMAT=15                                                 
 NOTE: The infile DUMMY is:                                                    
       Dsname=CHMICST.MPB.INPUT.TSV.SAYAN,                                     
       Unit=3390,Volume=BL3186,Disp=SHR,Blksize=4500,                          
       Lrecl=1500,Recfm=VB                                                     

NOTE: 1 record was read from the infile FILENMS.                              
      The minimum record length was 71.                                       
      The maximum record length was 71.                                       
NOTE: 0 records were read from the infile DUMMY.                              
NOTE: There were 1 observations read from the data set WORK.MACRO_FILES.      
NOTE: The data set WORK.EMPTY_FILES has 0 observations and 2 variables.       
NOTE: The DATA statement used 0.02 CPU seconds and 14642K.                                                                                                                                  

Solution
‎07-28-2017 06:27 AM
Respected Advisor
Posts: 3,799

Re: How to write a report if infile is empty

Posted in reply to sayanapex06

You need to use INFILE statement option END= and check it before you try to read any records.  For example.

 

28         filename FT15F001 temp;
29         parmcards;

30         ;;;;
31            run;
32         data _null_;
33            filevar = pathname('FT15F001','F');
34            infile dummy filevar=filevar END=EOF;
35            if eof then do;
36               putlog 'NOTE: EOF on ' Filevar= ;
37               end;
38            do while(not eof);
39               input;
40               putlog _infile_;
41               end;
42            stop;
43            run;

NOTE: The infile DUMMY is:
      (system-specific pathname), 
      (system-specific file attributes)

NOTE: EOF on filevar=system-specific pathname
NOTE: 0 records were read from the infile (system-specific pathname).
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
Contributor
Posts: 37

Re: How to write a report if infile is empty

Posted in reply to data_null__

Thanks a lot

Super User
Posts: 19,772

Re: How to write a report if infile is empty

Posted in reply to sayanapex06

You can use the methods suggested to check the file, but it's not a direct check of the file.

What are you checking the file for? Empty or non existence? Or will it have just a row header? I wonder if it's worth using FINFO type functions instead. 

 

 

Super User
Posts: 11,343

Re: How to write a report if infile is empty

Posted in reply to sayanapex06

sayanapex06 wrote:

Reeza I specifically mentioned infile not a sas dataset,.. i am reading the infile dummy from filevar.

 

Please help me to write a report if the dummy file is empty.

 

*****not a sas dataset*****


Please show exactly what the report for the empty table should look like.

 

You are expecting us to read your mind as to what the content of said report may be. You have been given a couple of examples but have not provided any detail as to why they are not acceptable.

☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 197 views
  • 0 likes
  • 6 in conversation