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
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
%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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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: