Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » How to determine if the external file is empty in SAS?

How to determine if the external file is empty in SAS?

BySubhro Posted onAugust 24, 2022August 24, 2022 Last Updated:August 24, 2022
0 Comments

The macro checks determine if the external file is empty in SAS. An error message is written to the log if it…

The macro checks determine if the external file is empty in SAS. An error message is written to the log if it doesn’t exist. SAS attempts to read the data from the file if the file exists. 

Data access functions like FOPEN, FREAD, and FGET are used to retrieve the data. The message is written to the log if there is no data to be read from the file.

%macro check_empty(outfile);
   %let filrf=myfile;
   %if %sysfunc(fileexist(&outfile)) %then %do;
   %let rc=%sysfunc(filename(filrf,&outfile));
   %let fid=%sysfunc(fopen(&filrf));
    %if &fid > 0 %then %do;
     %let rc=%sysfunc(fread(&fid));
     %let rc=%sysfunc(fget(&fid,mystring));
      %if &rc = 0 %then %put &mystring;
      %else %put file is empty;
     %let rc=%sysfunc(fclose(&fid));
    %end;
  %let rc=%sysfunc(filename(filrf));
  %end;
  %else %put file does not exist;
  %mend check_empty;

  %check_empty(/home/9to5sas/demo.txt)

The %LET statement is used to create a macro variable named &filrf that will contain the fileref name.

The %IF statement with the FILEEXIST function verifies that the file passed to the macro exists.

The FILENAME function associates a fileref with the file passed into the macro named &outfile.

We Use the FOPEN function to open that file and assign a file identifier value to the macro variable &fid.

To ensure that the FGET function was successful, we use the %IF statement. If the operation were successful, FGET would return 0, and we use a %PUT statement to write the contents of the variable named &mystring to the log.

The %PUT statement will write an empty message to the log if the %IF condition is false.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to Use Character Values on a Macro %DO Loop?
NextContinue
How to Print Empty SAS dataset information to the output window?

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You