- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
ERROR: Extension for physical file name "C:/Log.csv" does not correspond to a valid member type.
code:
%if(&did = 0) %then %do;
data _null_
file "C:/Log.csv" ;
run;
%end;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The data _null_ statement misses the semicolon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
(&did = 0)
ERROR: Skipping to next %END statement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
We need to see the whole log, so we can also check the creation of macro variable &did.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.