## Splitting String into Variables without delimiter

Solved
Regular Contributor
Posts: 160

# Splitting String into Variables without delimiter

Hi All,

I want to split a string into 5 Variables.4 variables should have 2 charcters and the 5 variable should contain remaining value

Var1 Var2 Var3 VAr4 Var5

PA    DI      NY   RD   FGHJ

Accepted Solutions
Solution
‎02-10-2017 06:20 AM
Super Contributor
Posts: 268

## Re: Splitting String into Variables without delimiter

Here you go:

``````data _null_;
length var1-var4 \$ 2
var5 \$ 4;
array vars[*] var1-var5;
startpos = 1;
do i = 1 to dim(vars);
length_var = vlength(vars[i]);
vars[i] = substr(longstring, startpos, length_var);
startpos + length_var;
end;
put _all_;
run;``````

I've made the code quite dynamic. The do-loop is not dependent on the length of any of the variables - if any of them were to be longer or shorter, the code would still work. The vlength function returns the length of the variable - there are a few functions like this (vnamevlabel et al).

All Replies
Solution
‎02-10-2017 06:20 AM
Super Contributor
Posts: 268

## Re: Splitting String into Variables without delimiter

Here you go:

``````data _null_;
length var1-var4 \$ 2
var5 \$ 4;
array vars[*] var1-var5;
startpos = 1;
do i = 1 to dim(vars);
length_var = vlength(vars[i]);
vars[i] = substr(longstring, startpos, length_var);
startpos + length_var;
end;
put _all_;
run;``````

I've made the code quite dynamic. The do-loop is not dependent on the length of any of the variables - if any of them were to be longer or shorter, the code would still work. The vlength function returns the length of the variable - there are a few functions like this (vnamevlabel et al).

Super Contributor
Posts: 474

## Re: Splitting String into Variables without delimiter

[ Edited ]

Hi.

Similar solution, with macro parameters.

`%let _SPLIT=2; * chars per var;%let _VARS=5; * max vars;data WANT;     S = 'PADINYRDFGHJ';     drop _:;     length VAR1-VAR%eval(&_VARS-1) \$&_SPLIT VAR&_VARS \$200;     array VARS[*] VAR1-VAR&_VARS;     do _I = 1 to &_VARS;        VARS(_I)=substr(S,(_I-1)*&_SPLIT+1);     end;run;`

SUBSTR function is used to split the string, and it's pretty much the best way for that.

Hope it helps.

Daniel Santos @ www.cgd.pt

Super User
Posts: 6,785

## Re: Splitting String into Variables without delimiter

[ Edited ]

All good ideas, but I will go the opposite route.  Here's the least flexible (but simplest) approach:

data want;

length var1-var4 \$ 2 var5 \$ 4;

array var {5};

do i=1 to 5;

var{i} = substr(longstring, 2*i - 1);

end;

run;

I don't think I read the previous reply closely enough.  There is really nothing new here, except removing the macro language.  However, if you are going to use the macro language approach, there is one tiny change that should be made.  The existing line:

`        VARS(_I)=substr(S,(_I-1)*2+1);`

`        VARS(_I)=substr(S,(_I-1)*&_SPLIT+1);`

Posts: 1,318

## Re: Splitting String into Variables without delimiter

``````data want;
length v1-v4 \$ 2 v5 \$4;
array v[5];
run;``````
Super Contributor
Posts: 268

## Re: Splitting String into Variables without delimiter

Poking? Ow!
Super Contributor
Posts: 474

## Re: Splitting String into Variables without delimiter

@Astounding

You are right!... Missed it, will edit the post... Thanks.

As for the the rest, I did say:

"Similar solution, with macro parameters."

"SUBSTR function is used to split the string, and it's pretty much the best way for that."

So yes, nothing new unless the flexibility of the macro parameters.

And by removing that flexibility, I think you'll get pretty much what you are proposing...

So maybe, there's not much new for both... kind of funny

Daniel Santos @ www.cgd.pt

Regular Contributor
Posts: 160

## Re: Splitting String into Variables without delimiter

Hi All,

Thanks Everyone for your help.My question feels silly.Hopefully one day will contribute answers to this community

I appreciate it

Super Contributor
Posts: 268

## Re: Splitting String into Variables without delimiter

Nah, it was quite a good question. And if you start experimenting with what SAS can do (and it's pretty good at playing around with strings), you'll be answering problems in no time!

Valued Guide
Posts: 2,191

## Re: Splitting String into Variables without delimiter

I think the easiest code to parse into 5 vars would be an input statement
Input @1 ( var1-var4 var5)( 4*\$2. \$10.) @@ ;
Of course your source string is not external. However that is no obstacle to using INPUT
This is not new..
There is a paper on the idea ... the title
SUGI 28: More _Infile_ Magic
www2.sas.com/proceedings/sugi28/086-28.pdf

☑ This topic is solved.