- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 11-03-2022 07:45 PM
(707 views)
Hi,
I have a variable which has values more than 200 in its length. So I am trying to take that variable until length 200 without chopping a word. Can you please help me?
Thanks,
Adithya
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
How do you know that the variable has more than 200 characters?
Are you able to define a variable that is 201 characters long, so you can inspect the 201st character?
Are you able to define a variable that is 201 characters long, so you can inspect the 201st character?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could just rebuild the string word by word and stop when it gets too long.
data want;
set have;
length short $200 ;
short = scan(long,1,' ');
do index=2 to 100 while (min(length(long),200)>=length(catx(' ',short,scan(long,index,' '))));
short = catx(' ',short,scan(long,index,' '));
end;
run;
Or ask FINDC() to find the place to break. Remember it is ok if the space is at position 201.
Example:
data test ;
input long $char40.;
length short $20 ;
short=long;
location=findc(long,' ',-21);
if location then short=substr(short,1,location);
cards;
----+---10----+---20----5---30----5---40
This is longer than 20 characters.
This one, is exactly 20 characters.
But this short.
;
Result
Obs long short location 1 ----+---10----+---20----5---30----5---40 ----+---10----+---20 0 2 This is longer than 20 characters. This is longer than 20 3 This one, is exactly 20 characters. This one, is exactly 21 4 But this short. But this short. 21
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%let len=20;
data test ;
input long $char40.;
cards;
This is longer than 20 characters.
This one, is exactly 20 characters.
But this short.
;
data want;
set test;
n+1;
length want $ 400 ;
do i=1 to countw(long,' ');
_want=want;
temp=scan(long,i,' ');
want=catx(' ',want,temp);
if length(want)>&len. then do;want=_want;output;want=temp; end;
end;
output;
drop i temp _want;
run;