hi,
I want to add quote to date value: 30sep2018 30sep2013
my code:
data _null_;
set ret_mon_1; ---------table has date value =30sep2018 and 30sep2013
call symputx('date_last',(put(date, date9.)));
call symputx('date_first',(%quote(put(first_date, date9.))));
put date first_date;
run;
%let date=%sysfunc(cats(%quote(&date_last),d));
%put &date;
I want to call 2 above dates : date='30sep2018'd first_date='30sep2013'd
I don't know why %quote cannot be used.
Broader observation first: you need to better understand the macro scanning process - assignments using & and % commands take place BEFORE data step execution. Please review macro documentation.
If your requirement is to add quotes to existing date value in a data set during execution, then macro assignments are unnecessary - the put() and cat() functions will suffice.
Sample code below. I'm assuming your dates are stored as numerics
data MyDates;
infile datalines;
input DateVal:date9.;
DateStr1=cat("'",put(DateVal,date9.),"'"); /* Surround date with single quotes */
DateStr2=cat('"',put(DateVal,date9.),'"'); /* Surround date with double quotes */
datalines;
30sep2018
30sep2013
run;
Macros are not needed here and in fact just make things much more complicated.
data _null_;
set ret_mon_1;
call symputx('date_last',input(date,date9.));
run;
Also:
I want to call 2 above dates : date='30sep2018'd first_date='30sep2013'd
This really doesn't mean anything to me, Call how? What code will you use them in?
@jojozheng wrote:
WHEN i %PUT &date_last
it will show in log : '30sep2018'd
I'm afraid I don't understand why the end goal is a %PUT statement.
Are you going to use this macro variable somewhere in the rest of your code? If so, you absolutely don't need the quote or the letter "d" at the end, you are working way to hard to accomplish something that is not needed. In that case, you need the macro variable to contain the SAS date value (which is an integer).
Several people have now told you that you don't need a macro variable to contain quotes, that there are simpler ways to get what you want.
You are working too hard.
First if you already have a macro variable with a value that is compatible with the DATE informat then you can use it directly as a date literal by adding the quotes and trailing d. You do NOT want to use single quotes since the macro variable reference will not resolve inside of single quotes.
%let date_last=30SEP2018;
%let date_literal="&date_last"d;
Second you don't need to use the human readable date literal syntax. You can just use the actual numeric value. So no need for quotes or PUT() function.
Try this code:
data _null_;
date='30SEP2018'd ;
first_date='30SEP2013'd;
call symputx('date_last',date);
call symputx('date_first',first_date);
call symputx('date_last_human',quote(put(date,date9.))||'d');
call symputx('date_first_human',quote(put(first_date,date9.))||'d');
put (date first_date) (=) ;
put (date first_date) (= date9.) ;
run;
%put &=date_last &=date_first;
%put &=date_last_human &=date_first_human;
Results:
date=21457 first_date=19631 date=30SEP2018 first_date=30SEP2013 NOTE: DATA statement used (Total process time): real time 0.18 seconds cpu time 0.01 seconds 1247 1248 %put &=date_last &=date_first; DATE_LAST=21457 DATE_FIRST=19631 1249 %put &=date_last_human &=date_first_human; DATE_LAST_HUMAN="30SEP2018"d DATE_FIRST_HUMAN="30SEP2013"d
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.