0
votes

I have two variables with me

%let End_of_month =%sysfunc(intnx(month,'&sysdate'd,-1,e),yymmdd10.); %stamp =00:00:00

Now, i want to create a new variable

%time_stamp = '2016-06-19 00:00:00'

In order to make this variable , i am using cat function with sysfunc

%timestamp =%sysfunc(cat("'","&End_of_month".,"&stamp.","'"));

The above code is not giving me the desired results. Could somebody tell me how do I solve this using a %sysfunc.

I am not really sure how to use %str and nstr in this statement. Please let me know how can we solve cats, cat and catx in a macro.

Thanks

3
Try to add 'dt' modifier %timestamp =%sysfunc(cat("'","&End_of_month".,"&stamp.","'dt")); if you want to use your code for date in textandrey_sz
Do you need the quotes? Macro code will treat those as part of the strings. And if need them do you really need to use single quotes? Single quotes suppress the evaluation of macro expressions. If you use double quotes then you can evaluate your macro variable.Tom
You probably shouldn't be using &sysdate either. That is the date that the SAS session started (and not necessarily the current date). You probably want to use %sysfunc(date()) to get the current date.Robert Penridge

3 Answers

1
votes

You initial 2 macro variable are wrong. Then End_of_month should rather be like this

%let End_of_month =%sysfunc(intnx(month,%str(%')&sysdate%str(%'d),-1,e),yymmdd10.);
%put &End_of_month;

OR

%let End_of_month =%sysfunc(intnx(month,%sysfunc(today()),-1,e),yymmdd10.);
%put &End_of_month;

Now coming to the creating a single quotation timestamp with 00:00:00 hr:min:sec afte the date with space as the delimiter, you don't have to use base SAS functions.. simply create the variable like below

%let timestamp=%str(%')&End_of_month 00:00:00%str(%');
%put &timestamp;

Also, %stamp means a macro call which has a definition and not a variable like you've mentioned in your questions.. similarly %timestamp= .. this is syntactically wrong in SAS.. %let timestamp= does macro variable creation and %timestamp would ideally call a timestamp named macro definition..I would suggest reading the SAS Macro Language Reference.

0
votes

The best way to solve this problem (if it is something you do often) is to create your own format to do so. See this answer:

https://stackoverflow.com/a/24044451/214994

0
votes

You do not need to use functions to concatenate macro variables. Just expand them where ever you need them in your code. Note also that you do not need to quote string literals in macro code. To the macro processor everything is a string literal.

%let end_of_month=%sysfunc(intnx(month,"&sysdate"d,-1,e),yymmdd10.);
%let time_stamp=&end_of_month 00:00:00;

Adding double quotes is simple.

%let time_stamp="&end_of_month 00:00:00";

Adding single quotes is a pain because the macro references are not resolved inside of single quotes. You could try using %bquote(), but then the value is macro quoted, which can cause trouble. So you might need to also add %unquote().

%let time_stamp=%unquote(%bquote('&end_of_month 00:00:00'));

I have found using the dequote() function is a simple way to introduce single quotes. Start with a string that has double quotes on the outside so that the macro references work and then use %sysfunc(dequote()) to remove the double quotes.

%let time_stamp=%sysfunc(dequote("'&end_of_month 00:00:00'"));