DATA Step, Macro, Functions and more

SQL: User as a variable / BUG

Reply
Frequent Contributor
Posts: 83

SQL: User as a variable / BUG

This caused some unexpected pain: one cannot use the variable USER in SQL:

 

data one ;

   user = 1 ;

run ;

 

proc sql ;

  select user

  from one

  ;

quit ;

 

proc sql ;

  select "user"n

  from one

   ;

quit ;

 

 

ƒƒƒƒƒƒ

kv1906

 

 

user

ƒƒƒƒƒƒƒƒ

1

 

I did not see that coming and it was suprising given how many data sets must have variable USER.

 

NOTE: SAS (r) Proprietary Software 9.4 (TS1M1)

NOTE: This session is executing on the X64_S08R2 platform.

 

NOTE: Additional host information:

X64_S08R2 WIN 6.1.7601 Service Pack 1 Server

 

 

Enjoy,

 

Kevin

Super User
Posts: 11,343

Re: SQL: User as a variable / BUG

Posted in reply to KevinViel

Documented under SQL reserved words:

 

The keyword USER is reserved for the current user ID. If you specify USER in a SELECT statement in conjunction with a CREATE TABLE statement, then the column is created in the table with a temporary column name that is similar to _TEMA001. If you specify USER in a SELECT statement without a CREATE TABLE statement, then the column is written to the output without a column heading. In either case, the value for the column varies by operating environment, but is typically the user ID of the user who is submitting the program or the value of the &SYSJOBID automatic macro variable.
Super User
Super User
Posts: 7,079

Re: SQL: User as a variable / BUG

[ Edited ]
Posted in reply to KevinViel

You will see that for a few special keywords that ANSI SQL defines and SAS supports. You will have similar problems in other implementations of SQL. The easiest fix is to add the alias to the variable name reference. In addition to using the name literal approach you can add the DQUOTE=ANSI option to PROC SQL and then it will treat values in double quotes as meaning the name of field.

 

Try this program

proc sql dquote=ansi ;
  select user 'Bare user'
       , "user" 'DQUOTED user'
       , 'user'n 'NLITERAL user'
       , a.user 'Alias included'
       , *
  from sashelp.class(obs=1 rename=(name=user)) a
  ;
quit;

 

Ask a Question
Discussion stats
  • 2 replies
  • 126 views
  • 6 likes
  • 3 in conversation