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

 Hi Everyone,

 

I have below data and I want to split the variable "VAR" into multiple variables after length of 9 .So after every 9th position in the string the sentence should split cutting words is fine. Please have a look at below datasets .Thank you in advance.

data have;

var="I want this text to split into variables with same length";

run;

 

Expected output:

var1              var2          var3        var4           var5            var6              var7
I want th     is text         to split     into vari      ables wit     h same le     ngth

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
   var="I want this text to split into variables with same length";
run;

data temp;
   set have;
   l = length(var);
   f = 1;
   do while (1);
      w = substr(var, f, 9);
      id + 1;
      output;
      f + 9;
      if f + 9 > l then do;
         w = substr(var, f);
         id + 1;
         output;
         leave;
      end;
   end;
run;

proc transpose data = temp out = want(drop = _:) prefix = var;
   id id;
   var w;
run;

 

Result:

 

var1       var2       var3       var4       var5       var6      var7 
I want th  is text t  o split i  nto varia  bles with  same len  gth 

View solution in original post

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
   var="I want this text to split into variables with same length";
run;

data temp;
   set have;
   l = length(var);
   f = 1;
   do while (1);
      w = substr(var, f, 9);
      id + 1;
      output;
      f + 9;
      if f + 9 > l then do;
         w = substr(var, f);
         id + 1;
         output;
         leave;
      end;
   end;
run;

proc transpose data = temp out = want(drop = _:) prefix = var;
   id id;
   var w;
run;

 

Result:

 

var1       var2       var3       var4       var5       var6      var7 
I want th  is text t  o split i  nto varia  bles with  same len  gth 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1 reply
  • 886 views
  • 0 likes
  • 2 in conversation