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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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