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

 

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

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

 

 

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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. 

Loko
Barite | Level 11

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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