BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a variable( around 250 char long) containing text. I wanted to break this text into 4 different variables say each containing around 60 characters. As I'll be printing this text on my report I wanted to break the text exactly between words( something like looking for a first space between letters 55-65, break the text and put it into Variable1 and do the same for rest of the text). I tried to do this with the help of index and scan functions and failed.

Any help is greatly appreciated.

Eddie
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Yes, the INDEX and SUBSTR functions will identify the offset where the previous or next blank begins, so you can assign your new SAS variables. Share what code you have already attempted to use for forum subscriber feedback/comment.

Scott Barry
SBBWorks, Inc.
GertNissen
Barite | Level 11
This is just a quick hack - has not been testet for all cases, but perhaps you can use it as an starting point for your own code.
[pre]
data longsmall(drop=txt t i text x);
length txt t $60;
array smalltxt(6) $60.;
text="I have a variable (around 250 char long) containing text. I wanted to break this text into 4 different variables say each containing around 60 characters. As I ll be printing this text on my report I wanted to break the text exactly between words";
do until (t=" ");
i+1;
t = scan(text,i," ");
if length(catx(" ",txt,t)) < 50 then
txt=catx('%20',txt,t);
else
do;
x+1;
put x= txt;
smalltxt(x)=txt;
txt = t;
end;
end;
x+1;
put x= txt;
smalltxt(x)=txt;
run;

Message was edited by: Geniz

replace %20 in the code with a normal space. The forum cuts my text when I use a space at this place - very strange???
DanielSantos
Barite | Level 11
You could try this also:

data _null_;

S='AAAA AAAA AAAA AAA AAAAA AAAAAA, AAAAA AAAA AAAAAAAAAAAAA BBB BBBBB BBBBB BBBBB BBBBBB BBBBB BBBBBBB BBBBBBBBBBBB CCCCCCC CCCCC CCCCCCC CCCCCCC CCCCC CCCC CCCCCCCCCC DDDD DDDDD DDDD DDDDDDD DDDDD DDDDDDDD DDDD DDDDD DDDDDD DDDDD DDDD';

I1=1*55+index(substr(S,55),' ')-1; /* 1st split starting at 55 */
I2=2*55+index(substr(S,2*55),' ')-1; /* 2nd split starting at 2x55 */
I3=3*55+index(substr(S,3*55),' ')-1; /* 3rd split starting at 3x55 */
VAR1=substr(S,1,I1);
VAR2=substr(S,I1,I2-I1);
VAR3=substr(S,I2,I3-I2);
VAR4=substr(S,I3);

/* result */
put VAR1=;
put VAR2=;
put VAR3=;
put VAR4=;
run;

Greetings from Portugal.

Daniel Santos at www.cgd.pt

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 743 views
  • 0 likes
  • 4 in conversation