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

Hi All,

I've got a dataset (not file) which has only one column and millions of rows. This column contains a fixed length data.

How can I split this one column into columns according to fixed length of new columns?

 

Example:

Is:

Dataset_1:

| Col1                      |

| AAABBBCCC111 |

| AAABBBCCC222 |

| AAABBBCCC333 |

 

Need:

Dataset_2:

| Col1 | Col2 | Col3 | Col4 |

| AAA | BBB | CCC | 111 |

| AAA | BBB | CCC | 222 |

| AAA | BBB | CCC | 333 |

 

Thanks,

 

Piotr

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    col1_new=substr(col1,1,3);
    col2_new=substr(col1,4,3);
    /* You type the rest, I'm lazy */
run;
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
data want;
    set have;
    col1_new=substr(col1,1,3);
    col2_new=substr(col1,4,3);
    /* You type the rest, I'm lazy */
run;
--
Paige Miller
Peter_Wine
Calcite | Level 5

Works, thanks.

 

I hoped that there is an easier method rather then typing "substring" n-times but Excel helped 🙂

 

Many thanks.

 

Piotr

PaigeMiller
Diamond | Level 26

Yes, there may be an easier solution if many more columns are involved, but for the example you gave, that's the solution I would use.

--
Paige Miller
data_null__
Jade | Level 19

Are all the fields length 3?  This could be modified to work with fields of different length

 

data test;
   input source $20.;
   cards;
AAABBBCCC111
AAABBBCCC222
AAABBBCCC333
;;;;
   run;
proc print;
   run;

data split;
   set test;
   array c[5] $3;
   pointer = addrlong(c1);
   *CALL POKELONG(source, pointer <, length> <, floating-point>);
   CALL POKELONG(source, pointer,length(source));
   run;
proc print;
   run;

Capture.PNG

 

ballardw
Super User

And a slightly less esoteric:

data test;
   input source $20.;
   cards;
AAABBBCCC111
AAABBBCCC222
AAABBBCCC333
;;;;

data want;
   set test;
   array c(4) $ 3;
   do i=1 to dim(c);
      c[i] = substr(source,(i-1)*3 +1, 3);
   end;
run;

but this does require the user to know how many variables to create.

Peter_Wine
Calcite | Level 5

Hi,

I just realized that using this solution I get as a result table which contains: old column and new columns.

So it looks that to old table new columns are added.

I want to keep only new columns.

 

And - I don't know if it is because of above - the length of new columns is set the same as length of old column. How to change it to appropriate length.

 

Please remember that my dataset has over 100 columns hence I ask to simple solution. 🙂

 

Thanks,

 

Piotr

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
  • 6 replies
  • 2104 views
  • 0 likes
  • 4 in conversation