BookmarkSubscribeRSS Feed
chinna0369
Pyrite | Level 9

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
Astounding
PROC Star
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?
Tom
Super User Tom
Super User

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

Ksharp
Super User
%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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 281 views
  • 0 likes
  • 4 in conversation