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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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