Instead of building NYSTR word by word, why not examine a copy of the source string at character 200 and work backwards until a blank or semi-colon character is found. Copy character 1 through the located character to NYSTR. Then move the rest of the copy-of-source-string to start in column 1, and repeat:
data tmp (drop=_:);
set db.cm (keep=subject cmtrt_product recordid);
_string=cmtrt_product;
length nystr $200;
do while (_string^=' ');
do _col=200 by -1 until (char(_string,_col) in (' ',';'));
end;
nystr=substr(_string,1,_col);
output;
_string=substr(_string,_col+1);
end;
run;
You really should show us what you expect for the output.
I would actually expect the use of the explicit delimiters you talk about in the SCAN and COUNTW functions. You only show using space as a delimiter. SCAN and COUNTW can use multiple delimiters such as this to use space and semicolon
do i = 1 to countw(cmtrt_product,' ;');
I might suspect that it would make more sense to just use the semicolon though so that the concepts are together in the output instead of split between multiple observations.
Instead of building NYSTR word by word, why not examine a copy of the source string at character 200 and work backwards until a blank or semi-colon character is found. Copy character 1 through the located character to NYSTR. Then move the rest of the copy-of-source-string to start in column 1, and repeat:
data tmp (drop=_:);
set db.cm (keep=subject cmtrt_product recordid);
_string=cmtrt_product;
length nystr $200;
do while (_string^=' ');
do _col=200 by -1 until (char(_string,_col) in (' ',';'));
end;
nystr=substr(_string,1,_col);
output;
_string=substr(_string,_col+1);
end;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: