BookmarkSubscribeRSS Feed
milanxiaoyao
Calcite | Level 5

Hi all

I have a quick question and I have been struggling for several days. I hope you could help me. Many thanks.

I hope to design a vector as follows: suppose there are two given vector a=[2 3 5], b=[1 2 3]
Here b indicates the frequency vector. I hope to generate a vector c=[2 3 3 5 5 5]. I am trying to use the repeat function in IML, but there is always something wrong

Thanks

2 REPLIES 2
Rick_SAS
SAS Super FREQ

Use the CUSUM function to get the indexes into a long vector that has sum(b) elements. Loop over the categories and use the REPEAT functions to fill up the vector:

proc iml;

a = {2 3 5};

b = {1 2 3};

cumFreq = 0 || cusum(b);

N = ncol(a);

v = j(sum(b), 1);

do i = 1 to N;

   bIdx = 1 + cumFreq; /* begin index */

   eIdx = cumFreq[i+1];   /* end index */

   print i bIdx eIdx;

   v[bIdx:eIdx] = repeat(a, b);

end;

print v;

milanxiaoyao
Calcite | Level 5

Hi Rick

     Many thanks.   This is great !    And the function cusum( ) is extremely helpful !

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