BookmarkSubscribeRSS Feed
Citrine10
Obsidian | Level 7

Hi there

 

I am trying to perform the below SQL code in a proc sql in SAS

 

SQL code:
select
reference
into
#Wi
from #all
where Reference in (select left(Reference ,len(Reference )-1)+ max(right(Reference ,1))
from stgw
group by left(Reference ,len(Reference )-1))
group by Reference , UCN1
order by ucn1

 

I tried:

proc sql;

create table Wi as

select
reference
from all
where Reference in (select left(Reference ,len(Reference )-1)+ max(right(Reference ,1))
from stgw
group by left(Reference ,substr(Reference, lenght(Reference)-1,0)))
group by Reference , UCN1
order by ucn1

 

Error:

ERROR: Function LEN could not be located.
ERROR: Expression using subtraction (-) requires numeric types.

 

How can I perform the length function in SAS PROC SQL?

2 REPLIES 2
Tom
Super User Tom
Super User

Please explain what the original query is actually doing. 

Your syntax appears be mixing strings and numbers. 

left(Reference ,len(Reference )-1) + max(right(Reference ,1))

The LEFT() function will move all of the leading spaces in a string to trailing spaces.

The RIGHT() function will move all of the trailing spaces in a string to leading spaces. 

Both only take one argument. 

The MAX() SQL aggregate function will take the string that collates last for the group. 

 

You then try to use addition with the resulting strings.

 

So what meaning does that strange code have in the original SQL dialect you were using?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 4923 views
  • 0 likes
  • 3 in conversation