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
;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 2261 views
  • 2 likes
  • 8 in conversation