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.
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;
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
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;
thanks for replay;
But, could you write the same code for sas, I am not familiar with sql.
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.
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.