| 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 |
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;
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;
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;
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;
Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.
Explore Now →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.