BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JasonNC
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

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 (vnamevlabel et al).

View solution in original post

9 REPLIES 9
LaurieF
Barite | Level 11

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 (vnamevlabel et al).

DanielSantos
Barite | Level 11

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

 

Astounding
PROC Star

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);

 

FriedEgg
SAS Employee
data want;
s='PADINYRDFGHJ';
length v1-v4 $ 2 v5 $4;
array v[5];
call pokelong(s,addrlong(v[1]),lengthn(s));
run;
LaurieF
Barite | Level 11
Poking? Ow!
DanielSantos
Barite | Level 11

@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

 

 

JasonNC
Quartz | Level 8

Hi All,

 

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

 

I appreciate it

LaurieF
Barite | Level 11

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!

Peter_C
Rhodochrosite | Level 12
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
Just load the _INFILE_ with your source value .
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

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
  • 9 replies
  • 5845 views
  • 6 likes
  • 6 in conversation