BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10

Hi Everyone,

 

running this code

data class_modified;
set sashelp.class;
rename name = User;
run;

proc sql;
create table work.test as select distinct
User 
from work.class_modified;
quit;

I get as a result from the PROC SQL-Query the "wrong" result (I get my SAS-userid displayed rather than the names of the sashelp.class data set variable "name") becasue the Enterprise Guide parser "thinks" that I want to use the automatic variable "User" rather than the actual variable I renamed.

Alread the syntax highlightning "tells" me this, as it displays the variable in blue rahter than as a black word:

blue_rather_than_black.JPG

 

When using a different procedure, e.g. PROC SORT, this does not happen (also the highlightning ist correct) :

proc_sort_user.JPG

Why does this happen?

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
JeffMaggio
Obsidian | Level 7

User is a reserved keyword in SQL. The same thing would happen if you have a column named 'from', 'select', etc.

View solution in original post

5 REPLIES 5
JeffMaggio
Obsidian | Level 7

The code you posted works fine for me with one minor adjustment.

 

If I were you, I would change the variable name as SAS reads the data in (on the set statement), though either way should work.

 

data class_modified;
set sashelp.class (rename=(name = User));
run;

 

For the second step, just explicitly tell SAS to use the User variable from the table:

proc sql;
create table work.test as select distinct
a.User 
from work.class_modified a;
quit;

 

FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10

Thank's for your reply, @JeffMaggio . As you pointed out, I could use an explicit ALIAS. I could also tell SAS to use the name literal 'User'n, which also works. I am just confused, as to why it is neccessary in PROC SQL and not necessary in other procedures like PROC SORT, for example....

JeffMaggio
Obsidian | Level 7

User is a reserved keyword in SQL. The same thing would happen if you have a column named 'from', 'select', etc.

Tom
Super User Tom
Super User

@JeffMaggio wrote:

User is a reserved keyword in SQL. The same thing would happen if you have a column named 'from', 'select', etc.


That is only partly right.  USER is not the same type of keyword as FROM or SELECT.

SAS will happily allow you to use FROM or SELECT as the name of a variable since it can tell from the position in the statement the role the word is playing.  But since USER is the name of a metadata variable (the username that is running the query) it is used in the same context as a normal variable name.  If you want to reference a variable named USER you just need to add some extra "syntactic sugar"  so that the compiler knows which USER you want.

proc sql ;
select
   user as username
  ,class.user as student_name
  ,select as student_age
  ,from  as student_height
from sashelp.class(obs=2 rename=(name=user height=from age=select))
;

 

Tom
Super User Tom
Super User

USER is a special keyword in PROC SQL.  To eliminate the confusion between this special meaning and a reference to a specific variable you can use an alias in your reference to the variable.  Something like this:

select a.user from mytable a ;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 808 views
  • 2 likes
  • 3 in conversation