BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Daniele96
Fluorite | Level 6
Hi everybody,
I am a new SAS user and I'd need your help.
I have a dataset having a single variable with multiple rows in each of which a string is contained.
This last must be divided according to a given positions and length, this data (positions and length) are contained in another dataset.
Example: Dataset containing positions and length:
LengthPosition
21
1

3

8

4


Dataset to be divided:
VAR1
abc0123def0
kol1256oiu1
The solution I found is to use the substr function for each variable, writing manually the positions and length:

 

data output;
    set input;
    var_1=substr(VAR1, 1, 2);
    var_1=substr(VAR1, 3, 1); 
    var_1=substr(VAR1, 4, 8);
    drop VAR1;
run;

 

but this method is slow, is there a faster and more automatic method? 
Thank you 🙂

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Ok. Try this

 

data one;
input Length Position;
datalines;
2 1 
1 3 
8 4 
;

data two;
input var1 :$12.;
datalines;
abc0123def0 
kol1256oiu1 
;
 
proc sql noprint;
   select count(*) into :d separated by ''
   from one;
quit;

data want(keep = var:);
   set two;

   v = Var1;

   do i = 1 to n;
      set one point = i nobs = n;
	  array var {&d.}; 
	  var[i] = substr(v, Position, Length);
   end;
run;

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Do you want this to be split into separate variables or observations ?

Daniele96
Fluorite | Level 6

Thanks for the reply, I'd be interested in spitting the dataset into separate variables.

PeterClemmensen
Tourmaline | Level 20

Ok. Is it always three variables? Or can that vary?

Daniele96
Fluorite | Level 6

It can vary.

PeterClemmensen
Tourmaline | Level 20

Ok. Try this

 

data one;
input Length Position;
datalines;
2 1 
1 3 
8 4 
;

data two;
input var1 :$12.;
datalines;
abc0123def0 
kol1256oiu1 
;
 
proc sql noprint;
   select count(*) into :d separated by ''
   from one;
quit;

data want(keep = var:);
   set two;

   v = Var1;

   do i = 1 to n;
      set one point = i nobs = n;
	  array var {&d.}; 
	  var[i] = substr(v, Position, Length);
   end;
run;
Daniele96
Fluorite | Level 6

WOW this is great!!! This code works on my original data, thank you 😊

PeterClemmensen
Tourmaline | Level 20

Anytime, glad to help 🙂

PeterClemmensen
Tourmaline | Level 20

If observations : 

 

data one;
input Length Position;
datalines;
2 1 
1 3 
8 4 
;

data two;
input VAR1 :$12.;
datalines;
abc0123def0 
kol1256oiu1 
;
 
data want(keep = var2);
   set two;

   do i = 1 to n;
      set one point = i nobs = n;
	  var2 = substr(var1, Position, Length);
	  output;
   end;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 8 replies
  • 2122 views
  • 2 likes
  • 2 in conversation