BookmarkSubscribeRSS Feed
goliPSU
Calcite | Level 5

Hi,

If I have a vector

v = [i

j

k]

I want to repeat a vector in a vector format couple of times:

v=[i

j

k

i

j

k

i

j

k

]

Actually I would like to use this in reshaping a matrix to a vector. but I want to add a column by the variable name as the first two columns:

z = [

1 2 3

4 5 6

7 8 9]

rowname = [ a b c]

calname = [i j k]

I am using Z = shape(Z, ncol*nrow, 1);

I want to create a table looks like:

a i 1

a j 2

a k 3

b i 4

b j 5

b k 6

c i 7

c j 8

c k 9

Thank you

1 REPLY 1
Rick_SAS
SAS Super FREQ

You want to use the REPEAT function, as discussed in the Getting Started chapter of my book.

Several examples are here: http://blogs.sas.com/content/iml/2012/06/27/create-an-id-vector-for-repeated-measurements/

For the first two columns, you should read about the very useful Define2DGrid function, which wraps up some of the REPEAT and SHAPE functionality: http://blogs.sas.com/content/iml/2011/01/21/how-to-create-a-grid-of-values/

You can use that function to generate the indices for the first two columns:

ij = Define2DGrid(1, 3, 3, 1, 3, 3);

i = ij[,1];  j = ij[,2];

rname={a b c};

cname = {i j k};

c1 = rname;

c2 = cname;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 1 reply
  • 1435 views
  • 0 likes
  • 2 in conversation