BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tzachi
Fluorite | Level 6

I have a set of vectors that i generate by randomly sampling from a larger vector (the item are words/characters).
I do it in a do loop and after each iteration i have a 1*4 or a 1*3 vector. I would like to collect all of these vectors into a matrix whose size is N*4, where N is the number of loops. In cases where the vectors are 1*3, the last item in a row of a matrix will be missing. I was hoping to initially create a N*4 matrix of zeros, and after each loop, insert the vector (either the 1*3 or 1*4) in row i, where i go from 1 to N.

 

Can anyone suggest an efficient code for implementing this task?

 

Thanks for your help,

Tzachi

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

It would have been useful for you to have posted your code.

 

Because you are storing character values, you can't allocate a numerical matrix of zeros.  Instead you need to allocate a character matrix of blank strings.  See the article "How to create a string of a specified length" for tips about how to create a matrix that contains a matrix that contains all blank strings. Then inside the loop you look at the number of elements of each sample and assign it to columns 1:3 or 1:4 of the i_th row, as follows:

 

 

proc iml;
call randseed(1234);
list = {peter piper picked a peck of pickled peppers};
N = 10;

/* determine number of items */
numItems = sample({3 4}, N);
print numItems;

/* Allocate matrix for results. See 
   http://blogs.sas.com/content/iml/2014/05/12/how-to-create-a-string-of-a-specified-length-in-sasiml.html */
results = j(N, 4, BlankStr(nleng(list)));
do i = 1 to N;
   items = sample(list, numItems[i]);
   results[i, 1:ncol(items)] = items;
end;
print results;

  

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

It would have been useful for you to have posted your code.

 

Because you are storing character values, you can't allocate a numerical matrix of zeros.  Instead you need to allocate a character matrix of blank strings.  See the article "How to create a string of a specified length" for tips about how to create a matrix that contains a matrix that contains all blank strings. Then inside the loop you look at the number of elements of each sample and assign it to columns 1:3 or 1:4 of the i_th row, as follows:

 

 

proc iml;
call randseed(1234);
list = {peter piper picked a peck of pickled peppers};
N = 10;

/* determine number of items */
numItems = sample({3 4}, N);
print numItems;

/* Allocate matrix for results. See 
   http://blogs.sas.com/content/iml/2014/05/12/how-to-create-a-string-of-a-specified-length-in-sasiml.html */
results = j(N, 4, BlankStr(nleng(list)));
do i = 1 to N;
   items = sample(list, numItems[i]);
   results[i, 1:ncol(items)] = items;
end;
print results;

  

Tzachi
Fluorite | Level 6

Thanks very much Rick!

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
  • 2 replies
  • 788 views
  • 1 like
  • 2 in conversation