Home > OS >  Why am i getting SAS error ERROR 22-322 (output a variable as string)
Why am i getting SAS error ERROR 22-322 (output a variable as string)

Time:03-24

I would like to output my variable in SAS EG server,

the variable "head" is "2022-02"

however, when I run below query, it generate "2020" which i suspect it process the formula 2022-2=2020. I have tried to use %bquote('&head.') & error pops up. I am not sure what i can do to output a column with variable value as "2022-02" from the variable "head"

variable:

%put head = &head.
head = 2022-02

1st attempt:

proc sql;
create table mart_base as 
select  *, &head. as test2
from     cr_card_acct_plan_seg_cycle
;quit;

it outputs "2020" for column test2

2nd attempt:

proc sql;
create table mart_base as 
select  *, &head. as test2, %bquote('&head.') as test1
from     cr_card_acct_plan_seg_cycle
;quit;

log as below

GOPTIONS ACCESSIBLE;
24         proc sql;
25         create table mart_base as
22: LINE and COLUMN cannot be determined.
NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 22-322: Syntax error, expecting one of the following: a quoted string, !, !!, &, *, **,  , ',', -, /, <, <=, <>, =, >, >=, ?, 
              AND, AS, BETWEEN, CONTAINS, EQ, EQT, FORMAT, FROM, GE, GET, GT, GTT, IN, INFORMAT, INTO, IS, LABEL, LE, LEN, LENGTH, 
              LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, OR, TRANSCODE, ^, ^=, |, ||, ~, ~=.  
26         select       *, &head. as test2, %bquote('&head.') as test1
NOTE: Line generated by the macro function "BQUOTE".
26         '2022-02'
           _
           22
            _
            200
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, (, *,  , -, BTRIM, CALCULATED, CASE, EXISTS, INPUT, NOT, PUT, SUBSTRING, TRANSLATE, USER, ^, ~.  

ERROR 200-322: The symbol is not recognized and will be ignored.

200: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 200-322: The symbol is not recognized and will be ignored.
27              from     cr_card_acct_plan_seg_cycle
28              ;

how can I output a column with the correct format of variable "head", which is 2022-02?

CodePudding user response:

You have two possibilities: Declare head as:

%let head = '2022-02'

Or decorate with quotes the variable in your code:

proc sql;
  create table mart_base as 
  select  *, "&head." as test2
  from  cr_card_acct_plan_seg_cycle;
quit;

CodePudding user response:

The macro quoting added by the %BQUOTE() macro function is confusing the SAS processor so that it is not recognizing the string literal.

Just use double quotes instead of single quotes so that the macro processor will resolve the macro variable reference.

"&head"

If you did need to use single quotes for some reason you could use %unquote() to remove the macro quoting that was confusing the SAS processor.

%unquote(%bquote('&head'))

Or use %SYSFUNC() macro function to call the SAS QUOTE() function to add the single quotes.

%sysfunc(quote(&head,%str(%')))
  • Related