DATA Step, Macro, Functions and more

How to handle with length truncation and replacing with valuable information

Accepted Solution Solved
Reply
Regular Contributor
Posts: 208
Accepted Solution

How to handle with length truncation and replacing with valuable information

I have a text string and with length set and if the string is more than the length it gets truncated in middle. Can i handle such a way if it truncates a word in middle than replace it with "etc."

 

data inc;
   length text $200.;	
   text="The man has a caregiver or an identified responsible person (for example, family member, social worker, case worker, or nurse) considered reliable by the nature in providing support to the headlines meeeting";
run;

This truncates -The man has a caregiver or an identified responsible person (for example, family member, social worker, case worker, or nurse) considered reliable by the nature in providing support to the headlines m

Can i replace "headlines m" with etc.. so that it makes some sense.

 

Any help on how to handle this


Accepted Solutions
Solution
2 weeks ago
Super Contributor
Posts: 331

Re: How to handle with length truncation and replacing with valuable information

Hello, I think this should work

 

data inc;
    length text $200. text196 txet $196.;	
    text="The man has a caregiver or an identified responsible person (for example, family member, social worker, case worker, or nurse) considered reliable by the nature in providing support to the headlines meeeting";

    if length(text)>196 then do;
        text=substr(text,1,197-index(reverse(substrn(text,1,196))," ") )||"etc.";
    end;
run;

View solution in original post


All Replies
Super User
Posts: 6,626

Re: How to handle with length truncation and replacing with valuable information

Bump up the length slightly:

 

data want;

length text $ 203;

text = " ........";

if length(text) > 200 then text = substr(text, 1, 195) || ' etc.';

run;

Regular Contributor
Posts: 208

Re: How to handle with length truncation and replacing with valuable information

Posted in reply to Astounding

I cannot do as the length of the string should only be 200 and not more than that. so need to adjust the data

Solution
2 weeks ago
Super Contributor
Posts: 331

Re: How to handle with length truncation and replacing with valuable information

Hello, I think this should work

 

data inc;
    length text $200. text196 txet $196.;	
    text="The man has a caregiver or an identified responsible person (for example, family member, social worker, case worker, or nurse) considered reliable by the nature in providing support to the headlines meeeting";

    if length(text)>196 then do;
        text=substr(text,1,197-index(reverse(substrn(text,1,196))," ") )||"etc.";
    end;
run;
Super User
Posts: 9,870

Re: How to handle with length truncation and replacing with valuable information

This will find the last blank from which it is possible to append the 'etc.' and don't cut a word in half right before it:

data want;
length newtext $200.;
text="The man has a caregiver or an identified responsible person (for example, family member, social worker, case worker, or nurse) considered reliable by the nature in providing support to the headlines meeeting";
newtext = text;
index = findc(newtext,' ','b');
if index > 195
then do;
  newtext = substr(newtext,1,index);
  index = findc(substr(newtext,1,index-1),' ','b');
end;
newtext = substr(newtext,1,index) !! 'etc.';
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 101 views
  • 1 like
  • 4 in conversation