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

Hi,

 

How do I use a substr in a Proc Sql Update?

 

For example, 

 

data test;
  input x $;
datalines;
abc
bbc
ccc
run;

data test;
   set test;

   substr(x,1,1) = 't';
run;

This will give me:
tbc
tbc
tcc

I tried using the substr in a PROC SQL Update but it won't work:

proc sql; 
  update test
  set substr(x,1,1) = 't';
quit;

1. How do I use the substr function in a Proc SQL Update.

2. Is it okay to use the same dataset in a set statement, like the dataset test in the above example.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Agreed with Paige, likely not possible to use this instance of SUBSTR (LEFT OF) in SQL.
You could use a combination of other functions to achieve the same output in SQL or you can use a data step.

Something like this perhaps:

proc sql;
update test
set x = catt( 't', substr(x, 2));
quit;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

While you can use the SUBSTR() function in PROC SQL, I don't think you can you can use the version of the SUBSTR() function on the left of the equal sign.

 

So, you most likely have to do this in a DATA step.

--
Paige Miller
Reeza
Super User
Agreed with Paige, likely not possible to use this instance of SUBSTR (LEFT OF) in SQL.
You could use a combination of other functions to achieve the same output in SQL or you can use a data step.

Something like this perhaps:

proc sql;
update test
set x = catt( 't', substr(x, 2));
quit;
Reeza
Super User
Unless you're working with a DB, SAS actually recreates the dataset when using an UPDATE statement so it's not particularly more efficient than other steps AFAIK. I think if you're working on a DB there are some functions that will be more efficient but YMMV.
Patrick
Opal | Level 21

@cosmid  Below code is fully working using a SQL Update.

data test;
  input x $;
  row+1;
datalines;
abc
bbc
ccc
;

proc sql;
  delete from test
  where row=2
  ;
quit;

proc sql;
  update test
  set x=catt('t',substrn(x,2))
  where row=3
  ;
quit;

proc contents data=test;
run;quit;

@Reeza wrote:
Unless you're working with a DB, SAS actually recreates the dataset when using an UPDATE statement so it's not particularly more efficient than other steps AFAIK. I think if you're working on a DB there are some functions that will be more efficient but YMMV.

@Reeza SQL changes tables in place where possible which is why deleted observations remain physically in the table.

Patrick_0-1633480283006.png

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1628 views
  • 6 likes
  • 5 in conversation