BookmarkSubscribeRSS Feed
jl2978
Fluorite | Level 6

I have a variable with values like

TUXEDO PARTNERS INC-AT
KEYES HOWARD AUTO DLRHSPS-PX
SPRING HILL FORD INC-CH
MEGASTAR INC
BRANDON CHRY-PLYM INC-TM

 

The ending "-XX" is the city abbreviations. I want to remove this part.

 

It can't be coded as removing characters after "-" because "-" can appear in the middle of the string. Or remove the last three characters because some values don't have ending "-XX". 

 

Any tips on dealing with this situation?

 

Thanks!

 

 

9 REPLIES 9
Reeza
Super User
Check if the 3rd to last character is a - and then remove it?

if substr(variable, length(variable)-3, 1) = '-' then want = substr(variable, length(variable)-3);
novinosrin
Tourmaline | Level 20
data have;
input str $40.;
cards;
TUXEDO PARTNERS INC-AT
KEYES HOWARD AUTO DLRHSPS-PX
SPRING HILL FORD INC-CH
MEGASTAR INC
BRANDON CHRY-PLYM INC-TM
;

data want;
 set have;
 _n_=findc(str,'-','b');
 if _n_ then want=substr(str,1,_n_);
 else want=str;
run;
smantha
Lapis Lazuli | Level 10

I hope you did not mean _n_ 🙂

ballardw
Super User

@smantha wrote:

I hope you did not mean _n_ 🙂


@novinosrin seems to like playing with fire and often uses a variable _n_ instead of considering the possible confusion with the SAS automatic variable of the same name.

Reeza
Super User
Most likely picked up from some of the older programmers I think but not something I would recommend doing at all 🙂
mklangley
Lapis Lazuli | Level 10

Using PRXMATCH to determine if the string ends with "-XX":

data have;
    input str $40.;
    cards;
    TUXEDO PARTNERS INC-AT
    KEYES HOWARD AUTO DLRHSPS-PX
    SPRING HILL FORD INC-CH
    MEGASTAR INC
    BRANDON CHRY-PLYM INC-TM
    TEST-ABC-DEF-XX
;
run;

data want;
   set have;
   if prxmatch('/(\w+)-\w\w\s*$/', str)
       then want = substr(str, 1, length(str)-3);
   else want=str;
run;
singhsahab
Lapis Lazuli | Level 10
data have;
input str $40.;
cards;
TUXEDO PARTNERS INC-AT
KEYES HOWARD AUTO DLRHSPS-PX
SPRING HILL FORD INC-CH
MEGASTAR INC
BRANDON CHRY-PLYM INC-TM
;
run;
data want;
set have;
rev_str=(strip(reverse(str)));
new_str=strip(reverse(substr(rev_str,index(rev_str,'-')+1)));
drop rev_str;
run;
Ksharp
Super User
data have;
input str $40.;

want=prxchange('s/-\w\w$//',-1,strip(str));

cards;
TUXEDO PARTNERS INC-AT
KEYES HOWARD AUTO DLRHSPS-PX
SPRING HILL FORD INC-CH
MEGASTAR INC
BRANDON CHRY-PLYM INC-TM
;
Ksharp
Super User
data have;
input str $40.;

want=prxchange('s/-\w\w\s*$//',1,str);

cards;
TUXEDO PARTNERS INC-AT
KEYES HOWARD AUTO DLRHSPS-PX
SPRING HILL FORD INC-CH
MEGASTAR INC
BRANDON CHRY-PLYM INC-TM
;

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 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
  • 9 replies
  • 1322 views
  • 2 likes
  • 8 in conversation