BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
huffa9299
Fluorite | Level 6

Hello,

 

I have a problem that I think should be simple but cannot quite get it to work. I need to remove the last word in a string.

 

Have:

ObsProduct
1Product A 1835
2Product B 201
3Product A 35
4Product B 4893

 

Want:

ObsProductProduct_Fam
1Product A 1835Product A
2Product B 201Product B
3Product A 35Product A
4Product B 4893Product B

 

is there a simple way to do this?

 

thank you for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input Obs Product $50.;
infile datalines dlm=',';
datalines;
1,Product A 1835
2,Product B 201
3,Product A 35
4,Product B 4893
;

data want(drop=pos length);
   set have;
   call scan(Product, -1, pos, length);
   Product_Fam=substr(Product,1,pos-2);
run;

View solution in original post

7 REPLIES 7
Reeza
Super User

Is it always numbers at the end? 

If so, can you apply the COMPRESS() function to the variable to remove the numbers?

 

productID = compress(product, , 'ka');
Number = compress(product, , 'kd');
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input Obs Product $50.;
infile datalines dlm=',';
datalines;
1,Product A 1835
2,Product B 201
3,Product A 35
4,Product B 4893
;

data want(drop=pos length);
   set have;
   call scan(Product, -1, pos, length);
   Product_Fam=substr(Product,1,pos-2);
run;
huffa9299
Fluorite | Level 6

Thank you Reeza and Draycut, both worked well, thank you for help!

novinosrin
Tourmaline | Level 20

Just for fun:

 

data have;
input Obs Product $50.;
infile datalines dlm=',';
datalines;
1,Product A 1835
2,Product B 201
3,Product A 35
4,Product B 4893
;

data want;
set have;
Product_Fam=substr(Product,1,length(product)-anyspace(reverse(strip(product))));
run;
ballardw
Super User

And another:

data have;
input Obs Product $50.;
infile datalines dlm=',';
datalines;
1,Product A 1835
2,Product B 201
3,Product A 35
4,Product B 4893
;
data want;
set have;
Product_Fam=strip(tranwrd(product,scan(Product,-1),''));
run;
Ksharp
Super User
data have;
input Obs Product $50.;
infile datalines dlm=',';
datalines;
1,Product A 1835
2,Product B 201
3,Product A 35
4,Product B 4893
;

data want;
 set have;
 want=prxchange('s/\w+$//',1,strip(Product));
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 16689 views
  • 1 like
  • 7 in conversation