BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mchan890
Calcite | Level 5
Value TEST - Group HS M228S Level 7  on SUPGRPPRF
Value TEST - Group HS M228S Level 6 - LF on SUPGRPPRF
Value TEST - Group HS M228S Level 6 - LF on GRP
Value TEST - Group HS M228S Level 6 - LF on GRP
Value TEST - Group HS M228S Level 6 - LF on GRP
Value TEST - Group HS M228S on SSGRP
Value TEST - Group HS M228S on SSGRP

 

I have the sample values above that needs to be trim from the left and right side. For example, I need the return values to be after the 3rd spaces from the left side and 2nd spaces from the right side.

 

Need return values:

Group HS M228S Level 7 
Group HS M228S Level 6 - LF
Group HS M228S Level 6 - LF 
Group HS M228S Level 6 - LF
Group HS M228S Level 6 - LF
Group HS M228S 
Group HS M228S

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Or if the data is as regular as it appears:

 

data want ;
  set have;
  str = strip(tranwrd(str,'Value TEST - ',''));
  str = substr(str,1,indexw(str,' on ')-2);
run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Perhaps someone can figure out a regular expression for this, but for now why not just parse the sting into words and then re-build it using the words you want to keep.

data have;
 input str $80.;
cards;
Value TEST - Group HS M228S Level 7  on SUPGRPPRF
Value TEST - Group HS M228S Level 6 - LF on SUPGRPPRF
Value TEST - Group HS M228S Level 6 - LF on GRP
Value TEST - Group HS M228S Level 6 - LF on GRP
Value TEST - Group HS M228S Level 6 - LF on GRP
Value TEST - Group HS M228S on SSGRP
Value TEST - Group HS M228S on SSGRP
;

data want ;
  set have;
  length new $80;
  do i=4 to countw(str,' ')-2; new=catx(' ',new,scan(str,i,' ')); end;
  drop i;
run;
proc print width=min;
run;
ballardw
Super User

Or if the data is as regular as it appears:

 

data want ;
  set have;
  str = strip(tranwrd(str,'Value TEST - ',''));
  str = substr(str,1,indexw(str,' on ')-2);
run;
PGStats
Opal | Level 21

Here is method based on regular expression matching:

 

data want;
if not prxId then prxId + prxparse("/Value TEST -\s+(.+)\s+on /i");
set have;
length value $80;
if prxmatch(prxId, str) then value = prxposn(prxId, 1, str);
drop prxId;
run;
PG

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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