Verify the existence of an external file
Conditionally execute code to read in a file only when the file exists.
Note: Although your operating environment utilities may recognize partial physical filenames, you must always use fully qualified physical filenames with FILEEXIST.
This example verifies the existence of an external file. If the file exists, read in the file using INFILE and INPUT statements. If the file does not exist, display a message in the SAS log that states the file does not exist.
Note that in a macro statement you do not enclose character strings in quotation marks.
/* If the file passed to the macro does exist, read in the file and create */
/* a character variable called VAR with a default length of 8 bytes. If */
/* file named in the macro call does not exist, write "FILE DOES NOT EXIST..." */
/* to the log. */
%macro in_file(file);
%if %sysfunc(fileexist(&file))=1 %then %do;
data a;
infile "&file";
input var $;
run;
%end;
%else %do;
data _null_;
put "FILE DOES NOT EXIST: &file";
run;
%end;
%mend;
/* Call the macro with file that does not exist */
%in_file(c:\bloom.txt);
/* Call the macro with file that does exist */
%in_file(c:\tracks\x1.txt);
OUTPUT
LOG when file does not exist
FILE DOES NOT EXIST: c:\bloom.txt
LOG when the file does exist ...typical notes seen when a file is read
NOTE: The infile "c:\tracks\x1.txt" is:
File Name=c:\tracks\x1.txt,
RECFM=V,LRECL=256
NOTE: 2 records were read from the infile "c:\tracks\x1.txt".
The minimum record length was 14.
The maximum record length was 20.
NOTE: The data set WORK.A has 2 observations and 1 variables.
source:www.support.sas.com
Welcome to StudySAS, your ultimate guide to clinical data management using SAS. We cover essential topics like SDTM, CDISC standards, and Define.XML, alongside advanced PROC SQL and SAS Macros techniques. Whether you're enhancing your programming efficiency or ensuring compliance with industry standards, StudySAS offers practical tips and insights to elevate your clinical research expertise. Join us and stay ahead in the evolving world of clinical data.
Discover More Tips and Techniques on This Blog
Subscribe to:
Post Comments (Atom)
Disclosure:
In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers.
My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.
No comments:
Post a Comment