BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vicky07
Quartz | Level 8

Hello,

i'm using the below code to check a file create date and later assign that date to a macro variable (moddate). The code works fine when file  exists but when there is no file in the declared path then I want moddate to be zero.

%let Filenm= c:\xyz.xls;

filename finput pipe %unquote(%str(%'dir "&filenm" /t:w /a:-d%'));

data _null_;

infile finput firstobs = 6;

input moddate ?? :mmddyy8. modtime ?? & time8.;

if moddate ne . then call symput('moddate',moddate);

run;

%put moddate = &moddate.;

i tried

if moddate ne  . then call symput('moddate',moddate);

else call symput('moddate',0);



Now, if I add the above else statement the moddate is giving zero even when the file exist. So how do I assign moddate to 0 only when the file doesn't exist.


Thanks in advance for your help!




1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You probably have trouble in your data step because DIR will output multiple lines of text.

To the specific question of how to set a default value you probably want to set the value BEFORE running the step that will find the value, if it exists.  Similar to how you would handle it when using the INTO clause in PROC SQL.

%let moddate=0;

data ....

if ... then call symputx('moddate',......);

run;

View solution in original post

5 REPLIES 5
Astounding
PROC Star

First, on a side note, you would be better off switching from SYMPUT to SYMPUTX.  That would avoid placing leading blanks on &MODDATE.

It looks like you have removed the messages that would give you a clue as to the source of the problem.  Most likely, the raw data you are piping in is not in the form that you expect (such as MMDDYY8.).  You can add this statement to the end of the DATA step to find out:

list;

It will list the data line on the .log file, so you can see what you are attempting to read.

The most likely explanation is that the date is coming in using some other format that cannot be interpreted as a date in MMDDYY8. form.  So the INPUT statement is generating a missing value, but the ?? suppresses any message about invalid data.

Good luck.

Tom
Super User Tom
Super User

You probably have trouble in your data step because DIR will output multiple lines of text.

To the specific question of how to set a default value you probably want to set the value BEFORE running the step that will find the value, if it exists.  Similar to how you would handle it when using the INTO clause in PROC SQL.

%let moddate=0;

data ....

if ... then call symputx('moddate',......);

run;

ballardw
Super User

I might suggest using the external file information functions instead of parsing output from DIR.

From online help the following would return the data elements available. Then you could use this to find the specific data elements of interest. Also there are FILEEXIST and FEXIST that let you determine if the file named exists.

data info;

  length infoname infoval $60;

  drop rc fid infonum i close;

  rc=filename('abc','c:\xyz.xls');

  fid=fopen('abc');

  infonum=foptnum(fid);

  do i=1 to infonum;

  infoname=foptname(fid,i);

  infoval=finfo(fid,infoname);

  output;

  end;

  close=fclose(fid);

run;

vicky07
Quartz | Level 8

Thank you all for your help!

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
  • 5 replies
  • 2792 views
  • 3 likes
  • 5 in conversation