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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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