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
For ex: PADINYRDFGHJ
Var1 Var2 Var3 VAr4 Var5
PA DI NY RD FGHJ
Here you go:
data _null_;
longstring = "PADINYRDFGHJ";
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 (vname, vlabel et al).
Here you go:
data _null_;
longstring = "PADINYRDFGHJ";
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 (vname, vlabel et al).
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
All good ideas, but I will go the opposite route. Here's the least flexible (but simplest) approach:
data want;
longstring='PADINYRDFGHJ';
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);
Instead, it should read:
VARS(_I)=substr(S,(_I-1)*&_SPLIT+1);
data want;
s='PADINYRDFGHJ';
length v1-v4 $ 2 v5 $4;
array v[5];
call pokelong(s,addrlong(v[1]),lengthn(s));
run;
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
Hi All,
Thanks Everyone for your help.My question feels silly.Hopefully one day will contribute answers to this community
I appreciate it
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!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.