What I\m trying to do seems simple but I've been trying to figure it out for the last hour and still nothing. I have a variable called NCCT_Date. What ever I pass to this variable I would like to populate every row of an already create dataset and call the column the data is getting passed to Rpt_Date. The code I'm using is shown below. When I run the code the Rpt_Date column is created but the 'Fall2021' data is not getting passed.
%let NCCT_Date = Fall2021;
data work.sampletbl;
set work.sampletbl;
Rpt_Date = &NCCT_Date;
run;
Why use a macro variable when you don't need to?
Assuming Rpt_Date is a character variable then you need to quote the macro variable
data work.sampletbl;
set work.sampletbl;
Rpt_Date = &NCCT_Date;
Rpt_Date1 = "Fall2021" ;
Rpt_Date2 = "&NCCT_Date";
run;
I need the macro because the "Fall2021" is used in several other places in the code. But putting quotes around the NCCT_Date works. I didnt think you could do that .
Macro variables perform text substitution when the code is executed. The value of the macro variable replaces the actual macro variable in your code upon execution. And the result MUST be valid working SAS code.
SO your line of code is:
Rpt_Date = &NCCT_Date;
and when you execute the code, it becomes
Rpt_Date = Fall2021;
So let me ask you, is this valid SAS code? Does it work, does it do what you want? If there is a variable in your SAS dataset named Fall2021, this works. But it doesn't assign the value of a text string Fall2021 to Rpt_Date, because you haven't created working SAS code when the macro variable value is substituted into your code. Do you see why this won't work to do what you want?
Also, you should first always create working SAS code without macros and without macro variables. It appears you didn't do that. This is always a mandatory first step ... if you don't have working SAS code, then code with macro variables will never work either.
I tried adding quotes to the macro so it could pass as 'Fall2021'. When I check this in the log it is resolving as expected but still will not pass correctly to the data step. I guess I'm asking what code do I need to write to pass 'Fall2021' correctly.
NCCT_Date2 = %quote(%')&NCCT_Date%quote(%') ;
%put &NCCT_Date2;
You need to understand how macro variables resolve.
Here's some good reading
Macro Variables Defined by Users
Here's the example with some comments added, but I recommend you read the documentation
512 options symbolgen mprint ;
513 %let NCCT_Date = Fall2021;
514
515 data _null_ ;
516 /* Resolves to
517 Rpt_Date = Fall2021;
518 */
519 Rpt_Date = &NCCT_Date;
SYMBOLGEN: Macro variable NCCT_DATE resolves to Fall2021
520 /* No macro resolution */
521 Rpt_Date1 = "Fall2021" ;
522 /* Resolves to
523 Rpt_Date2 = "Fall2021" ;
524 */
525 Rpt_Date2 = "&NCCT_Date";
SYMBOLGEN: Macro variable NCCT_DATE resolves to Fall2021
526 put rpt_date= rpt_date1= rpt_date2= ;
527 run;
NOTE: Variable Fall2021 is uninitialized.
Rpt_Date=. Rpt_Date1=Fall2021 Rpt_Date2=Fall2021
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
@Jabari wrote:
SpoilerI tried adding quotes to the macro so it could pass as 'Fall2021'. When I check this in the log it is resolving as expected but still will not pass correctly to the data step. I guess I'm asking what code do I need to write to pass 'Fall2021' correctly.
NCCT_Date2 = %quote(%')&NCCT_Date%quote(%') ;
%put &NCCT_Date2;
If you are using a macro function like %quote in a data step expect unexpected results. First there is a perfectly good non-macro function Quote (not needed).
Second if you use macro variables frequently you will find that attaching the quotes to the macro variable will quite often be more complex to debug than using the basic "¯ovariable." when trying to use it in data step, procedure or other statements.
Third you should show entire code of things that "don't work" and likely it should be from the LOG with all the notes and messages. For example your code above does not include a %let and does not appear in a data step so generates errors when attempting the assignment to NCCT_Date2.
In general you probably do not want the quote characters in the macro variable. But it depends how you are using the macro variable. Macro expressions will resolve inside of double quotes so just use those.
data want;
set have;
sas_variable = "¯o_variable":
run;
If you really need to add single quotes, perhaps because you are using the value in pass through SQL, then use %BQUOTE().
proc sql;
connect using mylib ;
create table want as select * from connection to mylib
(select * from sql_table where sql_variable = %bquote('¯o_variable')
)
;
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.