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

Can I convert the data in a table with one row to a table with multiple rows? What are their coding?

 

Original table:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 

Table that I want:

1    2   3   4

5    6   7   8

9   10 11 12

13 14 15 16

 


DSC_0834.JPG
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

In this case an Array is probably your best bet, with an explicit OUTPUT statement. Here's a quick example with two loops.

 

data want;
set have;

array vals(*) val1-val16;
array new(*) new1-new4;

do i=1 to 4;

    do j=1 to 4;
         new(j) = vals(j*i);
    end;

output;

end;
run;

 


@Jonathanzz wrote:

Can I convert the data in a table with one row to a table with multiple rows? What are their coding?

 

Original table:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 

Table that I want:

1    2   3   4

5    6   7   8

9   10 11 12

13 14 15 16

 


 

View solution in original post

6 REPLIES 6
art297
Opal | Level 21
data have;
  input var1-var4 @@;
  cards;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
;

Art, CEO, AnalystFinder.com

 

Reeza
Super User

In this case an Array is probably your best bet, with an explicit OUTPUT statement. Here's a quick example with two loops.

 

data want;
set have;

array vals(*) val1-val16;
array new(*) new1-new4;

do i=1 to 4;

    do j=1 to 4;
         new(j) = vals(j*i);
    end;

output;

end;
run;

 


@Jonathanzz wrote:

Can I convert the data in a table with one row to a table with multiple rows? What are their coding?

 

Original table:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

 

Table that I want:

1    2   3   4

5    6   7   8

9   10 11 12

13 14 15 16

 


 

ilikesas
Barite | Level 11

I guess that one way for this approach would be:

 

data want (keep = new1-new4);
set have;
array vals(*) var1-var16;
array new(*) new1-new4;

do j = 1 to floor(dim(vals)/4);
  do i = 1 to 4;
    new(i) = vals((j-1)*4 + i);
  end;
  output;
end;
run;
Ksharp
Super User

It is very easy for IML code.

 

data have;
  input var1-var4 @@;
  cards;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
;

proc iml;
use have;
read all var _all_ into x;
close;

want=shape(x,0,4);

create want from want;
append from want;
close;
quit;
CiCi
Fluorite | Level 6

I would probably

 

1. add a new observation with values like 1,2,3,4,1,2,3,4,1,2,3,4,....

2. use Proc Transpose

 

When your dataset is large, this can be super efficiant by avoiding loops.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1224 views
  • 6 likes
  • 6 in conversation