DATA Step, Macro, Functions and more

calculated character variable in proc sql

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

calculated character variable in proc sql

 

Calculated  is not working in character variable alias.

 

proc sql;
  select name as name1,
         calculated (substr(name1,1,2)) as test
    from sashelp.class;
quit;

 

 


Accepted Solutions
Solution
‎10-18-2016 07:34 PM
PROC Star
Posts: 1,759

Re: calculated character variable in proc sql

Posted in reply to SAS_inquisitive

Works fine for me:

proc sql;
  select upcase(NAME)                 as NAME1
        ,substr(calculated NAME1,1,2) as NAME2
  from SASHELP.CLASS;
quit;
NAME1 NAME2
ALFRED AL
ALICE AL
BARBARA BA
CAROL CA
HENRY HE
JAMES JA

 

 

 

 

 

View solution in original post


All Replies
Solution
‎10-18-2016 07:34 PM
PROC Star
Posts: 1,759

Re: calculated character variable in proc sql

Posted in reply to SAS_inquisitive

Works fine for me:

proc sql;
  select upcase(NAME)                 as NAME1
        ,substr(calculated NAME1,1,2) as NAME2
  from SASHELP.CLASS;
quit;
NAME1 NAME2
ALFRED AL
ALICE AL
BARBARA BA
CAROL CA
HENRY HE
JAMES JA

 

 

 

 

 

Super User
Super User
Posts: 7,942

Re: calculated character variable in proc sql

Posted in reply to SAS_inquisitive

Calculated is not a function, so your syntax:

calculated (<some code>) 

is invalid, it should read:

calculated <calculated variable name>

And then any further code should appear outside that statement.

 

This point is however moot as the code:

proc sql;
  select name as name1,
         substr(name,1,2) as test
    from sashelp.class;
quit;

Is in fact all you need, the calculated just adds more processing onto the whole thing so is a waste. 

Super Contributor
Posts: 308

Re: calculated character variable in proc sql

Posted in reply to SAS_inquisitive

name1 is not a calculated variable within the proc sql syntax. It is an alias you set to an already existing variable from the data file.

Calculated makes sens when you refer to a variable that it is generated in the proc sql.

 

Consider the following as an example:

 

proc sql;
  select name as name1,
         substr(name1,1,2) as test,
		  length(calculated test) as test1
    from sashelp.class;
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 369 views
  • 2 likes
  • 4 in conversation