BookmarkSubscribeRSS Feed
KevinViel
Pyrite | Level 9

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

2 REPLIES 2
ballardw
Super User

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.
Tom
Super User Tom
Super User

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;

 

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
  • 2 replies
  • 1367 views
  • 6 likes
  • 3 in conversation