Hello all,
Sorry for the confusing title -- not sure how to phrase it.
In the code below, I'm looking into a Netezza table (Data step won't work) and deleting current period records if they exist. The issue is, how to represent the value of 202212 as ym_id is a character/text field (don't ask me, I didn't create the database).
Would anyone help me please with this macro issue?
Thank you
/******** CODE IN QUESTION **************/
%let period = 202212;
%let qperiod = %str(')&period%str(');
proc sql noerrorstop;
delete from core.mytable where ym_id = .
quit;
There are cases within SQL where a quoted quote causes a problem. If all the above suggestions are still not giving you a solution, try:
%let qperiod = %unquote(%str(%')&period%str(%'));
and be sure to use &qperiod where needed, not &period,.
You can't have imbalanced quotes inside %STR() See https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0pnc7p9n4h6g5n16g6js048nhfl.htm
You can have imbalanced quotes if you use the % operator inside %STR()
%let qperiod = %str(%')&period%str(%');
The simple solution is to put the quotes into the macro variable to begin with:
%let period = '202212';
If you are building the macro variable with SAS code use the QUOTE function to add the single quotes.
data _null_;
call symputx('period',quote(put(today(),yymmn6.),"'");
run;
If you really really have to have the macro variable without quotes then use the %BQUOTE() function to add them.
%let period=202212;
...
where ym_id = %bquote('&period')
...
1894 %let period=202212; 1895 %put 1896 where ym_id = %bquote('&period') 1897 ; where ym_id = '202212'
In a large percentage of cases it is better not to make quotes part of the macro variable at all. Use double quotes around a macro variable to allow it to resolve:
%let period = 202212; proc sql noerrorstop; delete from core.mytable where ym_id= "&period."; quit;
That means for the places where &period may not want quotes you don't need another variable to have that available.
With that said if period 202212 is supposed be December of 2022 then you may want to rethink entirely how that is stored. That really should be a date value and character values holding dates almost always require using date values for manipulation unless it is trivial. For example what if you want to represent a duration of 3 sequential months?
You can't use something like
ym_id in ("&period" "%eval(&period. +1)" "%eval(&period. +2") )
because +1 would be 202213, not the desired 202301.
There are cases within SQL where a quoted quote causes a problem. If all the above suggestions are still not giving you a solution, try:
%let qperiod = %unquote(%str(%')&period%str(%'));
and be sure to use &qperiod where needed, not &period,.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.