DATA Step, Macro, Functions and more

Delete the last words of a sentence

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Delete the last words of a sentence

Hello. 

I have a problem on deleting the last words of a sentense.

So, here is the details of the problem:

 

the column contains models of phones with colors, I must eliminate the colors and needless details from it, there are cases when the models contain two type of colors or details, for instance 

Alcatel One Touch POP C7 Bluish Black
iPhone 6 S 32 GB Full Gold

I want to delete the last two "words" if they are colors or details. how to do it?

 

thank you a lot.


Accepted Solutions
Solution
‎07-07-2017 09:11 AM
Super User
Posts: 6,941

Re: Delete the last words of a sentence

data want;
set have;
newstring = string; /* to get the same length */
newstring = ' ';
do i = 1 to countw(string) - 2;
  newstring = catx(' ',newstring,scan(string,i));
end;
drop string;
rename newstring=string;
run;
  
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Contributor
Posts: 33

Re: Delete the last words of a sentence

Alcatel One Touch Idol 2 Mini S Slate White
Alcatel One Touch POP C5 Dark Grey
Alcatel One Touch POP C5 Pure White
Alcatel One Touch POP C7 Bluish Black
Alcatel One Touch POP C7 Full White

Super User
Super User
Posts: 7,403

Re: Delete the last words of a sentence

And what is the logic for know that either the last, or last two words are to do with color?  "Full" for instance has nothing logically to do with color.  I would suggest you build a list of strings, or combination of possibles, and use that to remove information from the string, e.g.:

data colors;
  color="SLATE WHITE"; output;
  color="DARK GREY"; output;
  color="GREY"; output;
...
run;
proc sql;
create table WANT as
select tranwrd(A.STRING,B.COLOR) as STRING
from HAVE A
left join COLORS
on index(A.STRING,B.COLOR) > 0;
quit;

 

Contributor
Posts: 33

Re: Delete the last words of a sentence

thanks for replay;

 

But, could you write the same code for sas, I am not familiar with sql.

Super User
Super User
Posts: 7,403

Re: Delete the last words of a sentence

Not without a lot of code.  You will see the join is on the value of one being within the string of the other (the index()).  This is difficult to do in base SAS.  Its really worth learning SQL, its relatively simple and very powerful on joins.

Solution
‎07-07-2017 09:11 AM
Super User
Posts: 6,941

Re: Delete the last words of a sentence

data want;
set have;
newstring = string; /* to get the same length */
newstring = ' ';
do i = 1 to countw(string) - 2;
  newstring = catx(' ',newstring,scan(string,i));
end;
drop string;
rename newstring=string;
run;
  
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 33

Re: Delete the last words of a sentence

 can I use an array if I must check if the last two words are needless words? Suppose I have the list of the "words" that must be deleted from the string.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 149 views
  • 3 likes
  • 3 in conversation