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-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!

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
  • 843 views
  • 0 likes
  • 2 in conversation