SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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