how to create single variable to multiple columns

Reply
Contributor
Posts: 26

how to create single variable to multiple columns

Dear all,

     Suppose i,ve a dataset containing one variable with observations from 1 to 50 (lets say). Now i need to print the output like given bellow

  subno

  1      11      21    

  2      12      22

  3      13      23

  4      14      24

  5      15      25

  6      16      26

  7      17      27

  8      18      28

  9      19      29

  10     20     30  . . . .

                         Thanks in advance.

Esteemed Advisor
Esteemed Advisor
Posts: 6,704

Re: how to create single variable to multiple columns

I suppose this ties in with your other post:

Your request is pretty simple, just merge the same dataset back for each block of 10 records, look at noobs, point and such like. 

However why would you want to, you don't seem to have any ID variables to merge by, i.e. why should 11 merge to 1 and not 2?  For your example data it makes no sense to have any merging or dataset, just do that in a loop:

data temp (drop=i j);

  array var{3} 8.;

  do j=1 to 10;

    do i=1 to 3;

      var{i}=((i-1) * 10) + j;

    end;

    output;

  end;

run;

Respected Advisor
Posts: 3,107

Re: how to create single variable to multiple columns

This kind of data manipulation always makes me consider PROC TRANSPOSE. Not knowing your exact purpose (if we know what you are up to, the answer could be completely different from the beginning), here is one generic approach, I didn't use VIEW which I should, for the sake of better visibility.

options validvarname=any;

data test;

     do num=1 to 100;

           output;

     end;

run;

/*Say you want 7 records per group*/

data step_1;

     do i=1 to 7 until (last);

           set test end=last;

           grp=_n_;

           output;

     end;

run;

proc sort data=step_1 out=step_2;

     by i;

run;

proc transpose data=step_2 out=want(drop=i _name_) let;

     by i;

     id grp;

     var num;

run;

Post a Question
Discussion Stats
  • 2 replies
  • 2016 views
  • 0 likes
  • 3 in conversation