4

I have a parameter start_date in a SAS DIS job of type text, and I have set a default value. If the default value is something like 01JAN2015 (with no quotes) then referencing the variable like

input("&start_date",date9.)

works fine. However, if I give it the default value put(today(),date9.) then the value held by the parameter is the instruction string rather than the string that the instruction should evaluate to. Thus references to the parameter do not behave as desired.

I've tried using %EVAL and EVALUATE, but all such functions seemed to be designed for evaluating arithmetic expressions represented as strings. Is there any way to either get this expression evaluated, or to properly encode it to begin with?

(FYI, the reason I am using a text parameter rather than a date parameter is that trying to use date parameters did not work for me when I tried to loop over the parameterized job, which is my intention here).

Much gratitude.

Joe
  • 62,789
  • 6
  • 49
  • 67
Rookatu
  • 1,487
  • 3
  • 21
  • 50
  • this is weird that you are not able to loop based on date parameter. i thought that problem was resolved. Can you post your test data, variable type and format on the loop transformation question and i'll try and update my answer there. Also, default value in parameter should be hardcoded value instead of function or evaluations. Using macro variable is fine since that is equivalent to harcoding, the only difference being that it is calculated before loop transformation. For setting default value based on evaluation, control table should have precalculated information. – sushil May 06 '15 at 05:04
  • Hey sushil. In the post on looping over date parameters I mentioned that for some reason the dates had to be encoded as text. I haven't figured out any way around this. Trying to use date type parameters and looping yields errors on the loop transformation such as "ERROR: Character expression requires a character format". Do you know a way around this? Thanks – Rookatu May 25 '15 at 14:55
  • yeah, i understand that would happen. why don't you try using USER WRITTEN transformation and create CONTROL TABLE based on your requirement and connect that to the Loop transformation and Accordingly apply the parameter value in the parameterized job flow of the same type. I see that you are trying to do on the fly calculation on the parameter value for the loop. Would suggest moving that calcuated value in the CONTROL TABLE and then use it from there directly. If this doesn't help then i'll try post snapshots here : http://stackoverflow.com/q/29855816/4653284 – sushil May 26 '15 at 08:35
  • I've noticed some strange behaviour: following your advice I have changed the date parameters to date types instead of text, but when I set the default values (whether hard coding or using the drop down menu to choose a date) I get the error: "Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=." Any idea why this happens or how to avoid it? – Rookatu Jun 02 '15 at 15:44
  • did you get this error while setting default value in the parameterized job? If i remember correctly, a calendar icon shows up next to value selection when date type is selected and that should not show this kind of error when any date is selected. If this error shows up at value selection then this is indeed wierd. Can you try creating a new parameter and then try with date parameter. perhaps that should help. i'm sorry, i can't be much help on this error. – sushil Jun 03 '15 at 08:06
  • Hey Sushil. Based on the nature of this error I'm guessing it is a bug in the software. I did try creating new parameters, with the date type, and still got this error. Now my approach is to try to check using code whether a parameter is missing, and if so, set the value of a date variable to whatever default I would have chosen. Thanks for your help though! – Rookatu Jun 08 '15 at 15:06

1 Answers1

3

You need to use %sysfunc to execute data step functions with the macro processor. This also eliminates the need for a put() function as %sysfunc allow specifying a format for the returned value:

%let start_date=%sysfunc(today(),date9.);
%put &start_date;

Or you could use a data step to evaluate the value:

data _null_;
  call symput('start_date',put(&control_start_date,date9.));
run;
%put &start_date;
DWal
  • 2,752
  • 10
  • 19
  • When I apply this using `%let start_date = %sysfunc(putn(%sysfunc("&control_start_date),date9.));` I get the error "Function name missing in %SYSFUNC or %QSYSFUNC macro function reference". Any way to get this to work with `today()` replaced with a parameter which contains the text "today()-5" say? – Rookatu May 05 '15 at 15:39
  • You could use the `tranwrd` function to search for the string "today()" and replace it with "%sysfunc(today())": `%let start_date = %sysfunc(putn(%sysfunc(tranwrd(&control_start_date,today(),%sysfunc(today()))),date9.));` – DWal May 05 '15 at 16:17
  • This is a bit problematic since I want to evaluate general expressions within parameters (e.g. "today()-5"). It is helpful though since by wrapping `sysfunc()` around just "today()" when defining the parameter I can now at least get a character representation of an integer (presumably the internal SAS date value) with `start_string = "&control_start_date"` - however, thereafter letting `start_date = input(trim(start_string),date9.)` does not produce any output. I'm almost there! Any ideas? Thanks again – Rookatu May 05 '15 at 18:22
  • If your input parameters can vary so much you could just add a data step to evaluate the expression then `call symput` to assign the start_date value (see edit above). – DWal May 05 '15 at 19:42
  • @DWal, for the 1st %let statement i wanted to let you know that in case of today() function usage instead of the %let you've created you can use : %let start_date=%sysfunc(today(),date9.); . You can add that if you want, so that others may benefit. Also, wanted to point out that in case of getting today() as date9. , we could have just used SYSDATE9. automatic macro variable. – sushil May 06 '15 at 04:52
  • That's a good point; I didn't know you could put a format in %sysfunc(). As far as SYSDATE9, OP is asking about how to use parameters `today()` and `today()-5`. I'm not questioning why he needs that, but SYSDATE9 doesn't help in this case. – DWal May 06 '15 at 13:04