BookmarkSubscribeRSS Feed
Citrine10
Obsidian | Level 7

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;
6 REPLIES 6
Citrine10
Obsidian | Level 7
Hi I tried that but got an error:
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;
Citrine10
Obsidian | Level 7
After adding the semicolon it gives this error:
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.
Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1689 views
  • 1 like
  • 3 in conversation