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:
Obs | Product |
1 | Product A 1835 |
2 | Product B 201 |
3 | Product A 35 |
4 | Product B 4893 |
Want:
Obs | Product | Product_Fam |
1 | Product A 1835 | Product A |
2 | Product B 201 | Product B |
3 | Product A 35 | Product A |
4 | Product B 4893 | Product B |
is there a simple way to do this?
thank you for your help!
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;
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');
product = substr(product,1,length(product)-length(scan(product,-1,' ')));
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;
Thank you Reeza and Draycut, both worked well, thank you for help!
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;
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;
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.