hi
I check a directory to see if there are files. If there are no files I create a csv file in it however when creating the file it populates the results of checking the directory. Is there a way to create an empty file or empty the contents of my csv file?
data _null_ ; filename pathdir "%C:/FileCheck"; did = dopen("pathdir"); numfiles =dnum(did); rc = dclose(did); run; %if(&did = 0) %then %do; proc export outfile="C:/Log.csv" dbms=tab; run; %end;
Use the same technique I showed you here.
A simple FILE statement without a PUT creates an empty file:
data _null_;
file "C:/Log.csv";
run;
The data _null_ statement misses the semicolon.
We need to see the whole log, so we can also check the creation of macro variable &did.
Your current program appears to be running a data step to check if a directory exists.
It then tests some un-related macro variable that you don't show where it came from to decide whether or not to run PROC EXPORT.
I cannot tell from your question what you actually want to happen. If the dataset has zero observations then PROC EXPORT will not create an empty file. It will write a file that only contain the header row. So not empty, just one with zero lines of actual data. Is the goal to not write that header line? Is the goal to not create the file at all? What about if there was already a file at that location? Do you want to erase the existing file?
Or is the question about the logic errors in your program (you are referencing a macro variable named DID that you never created).
Or the call to the macro named C that you have not provided any definition for. Or is that just a typo.
You are using C: in filenames implying WINDOWS/DOS operating system but then using / as the delimiter between the directories instead of \. (Note: SAS will silently fix the / for you converting them into \ when actually reading/writing from the Windows filesystem). Is that part of the issue or just typos when making dummy names to share on-line?
Note: Here is a simple macro to test if a directory exists: %direxist()
So perhaps you want to run something like this?
%if %direxist(/some/directory/) %then %do;
proc export
outfile="/some/file"
dbms=tab;
run;
%end;
Note: A CSV file is a Comma Separated Values file. If you are going to use TAB as your delimiter you might want to use a different extension on the filename.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.