DATA Step, Macro, Functions and more

splitting data to differnt Rows

Reply
Regular Contributor
Posts: 229

splitting data to differnt Rows

Splitting the data in to differnt rows id name      value2              value3 1  a,b      a:1,b:1              a:2,b:2 2  a,b      a:3,b:4              a:3,b:4 3  a        a:5                  a:6 4  a,b,c,d  a:10,b:20 c:30 d:40  a:10,b:20 c:30 d:40 Output I am having a variable Name it should split base on the commas and the correspoding value(value2 and value3) sould come. id Name value2    value3 1  a  a:1    a:2 1  b  b:1    b:2 2  a  a:3    a:3 2  b  b:4      b:4 3  a  a:5    a:6 4  a  a:10    a:10 4  b  b:20    b:20 4  c    c:30    c:30 4  d    d:40    d:40 For ef you can view in txt

Attachment
Super User
Posts: 10,046

Re: splitting data to differnt Rows

EASY.

data have;
input id name & $20.     value2  & $20.            value3 & $20.;
cards;
1  a,b      a:1,b:1              a:2,b:2
2  a,b      a:3,b:4              a:3,b:4
3  a        a:5                   a:6
4  a,b,c,d  a:10,b:20 c:30 d:40   a:10,b:20 c:30 d:40 
;
run;
data want(drop=value: i n name);
 set have;
 length _name _value2 _value3 $ 20;
 n=max(countw(value2,', '),countw(value3,', '));
 do i=1 to n;
  _name=scan(name,i,', ');
  _value2=scan(value2,i,', ');
  _value3=scan(value3,i,', ');
  output;
 end;
run;

Ksharp

Ask a Question
Discussion stats
  • 1 reply
  • 155 views
  • 0 likes
  • 2 in conversation