Hi Experts,
I have a character variable with long values.
VarName Value
description 'Fair weather expected todaybut tomorrow's will be inclement' (please notice todaybut and absence of full stops/periods)
I have imported the data from an excel (97-2003) worksheet and applied LEFT, UPCASE AND COMPRESS functions to clean and left align it in SAS table.
When I tried separating the conjoined todaybut and add periods to modify it ('Fair weather expected today. But tomorrow's will be inclement.
if description= 'Fair weather expected todaybut tomorrow's will be inclement'
then description= 'Fair weather expected today. But tomorrow's will be inclement.' ; run;
It did not work and gave me the original value(s).
Is there another better way of handling this issue? Thanks in advance.
how about these code?
data have;
length description $200;
description='Fair weather expected todaybut tomorrow''s will be inclement';
run;
data want1;
set have;
if description='Fair weather expected todaybut tomorrow''s will be inclement' then
description='Fair weather expected today. But tomorrow''s will be inclement.';
run;
data want2;
set have;
description=strip(tranwrd(description,'todaybut','today. But'))||'.';
run;
They both work.
Thanks for suggestion. That value is one of several values. However, does not solve the problem.
For a generalized solution, consider using an external spellchecker. Writing SAS code to detect syntactical errors in free text is not a promising task, to say the least.
Since you have a single quote in the text, you must use double quotes around the text:
data have;
length description $100;
description = "Fair weather expected todaybut tomorrow's will be inclement";
if description = "Fair weather expected todaybut tomorrow's will be inclement"
then description = "Fair weather expected today. But tomorrow's will be inclement.";
run;
One issue with attempting to insert characters into an existing variable is that depending on the actual length of a specific value and the length of the value after "inserting" you can exceed the defined length and end up with truncated data.
Another way instead of testing for along string is to use Tranwrd.
data example; length x $ 100; /* make sure the example has long enough value to insert*/ /* if you skip this then X length is defined using the value shown*/ x = "Fair weather expected todaybut tomorrow's will be inclement"; x = tranwrd(x,'todaybut','today. But'); run;
If you have very many records and/or variables this becomes extremely tedious and you would want to use a tool intended to fix such stuff.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.