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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Yeah, that view is really only good for display, formats are not needed. PUT converts the numeric date to a character variable which is adding work for yourself. If you leave it as a SAS date, which is a number, it will be easier to work with.

View solution in original post

8 REPLIES 8
Reeza
Super User
Use the QUOTE not %QUOTE function. Also, if you don't format the dates (using PUT), it will work as desired without any formats.
jojozheng
Quartz | Level 8
thank you @Reeza
you mean like below:
call symputx('date_last',(put(date)));
or can you specify for me?
AndrewHowell
Moderator

@jojozheng.

 

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;

 

 

 

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
jojozheng
Quartz | Level 8
WHEN i %PUT &date_last
it will show in log : '30sep2018'd
Reeza
Super User
Yeah, that view is really only good for display, formats are not needed. PUT converts the numeric date to a character variable which is adding work for yourself. If you leave it as a SAS date, which is a number, it will be easier to work with.
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 9596 views
  • 0 likes
  • 5 in conversation