BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vraj1
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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

4 REPLIES 4
Astounding
PROC Star

Bump up the length slightly:

 

data want;

length text $ 203;

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

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

run;

vraj1
Quartz | Level 8

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

gamotte
Rhodochrosite | Level 12

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;
Kurt_Bremser
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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