DATA Step, Macro, Functions and more

How to assign missing macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

How to assign missing macro variable

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!





Accepted Solutions
Solution
‎10-23-2012 12:00 PM
Super User
Super User
Posts: 6,499

Re: How to assign missing macro variable

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


All Replies
Super User
Posts: 17,784

Re: How to assign missing macro variable

Super User
Posts: 5,080

Re: How to assign missing macro variable

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.

Solution
‎10-23-2012 12:00 PM
Super User
Super User
Posts: 6,499

Re: How to assign missing macro variable

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;

Super User
Posts: 10,483

Re: How to assign missing macro variable

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;

Contributor
Posts: 71

Re: How to assign missing macro variable

Thank you all for your help!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 292 views
  • 3 likes
  • 5 in conversation