SAS Data Science

Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Viya (Machine Learning), SAS Visual Text Analytics, with point-and-click interfaces or programming
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 17396 views
  • 1 like
  • 7 in conversation