BookmarkSubscribeRSS Feed
thiagoavadore
Calcite | Level 5
Hello there!

I'm doing a code for aleatorization test on my graduation and I'm stuck in one thing.
I have a column vector and I wanna change the order (randomly) of the number os my vector... how can I do it on IML?

data tensao1;
input tensao @@;
cards;
16.85 16.40 17.21 16.35 16.52
17.04 16.96 17.15 16.59 16.57
;
run;
data tensao2;
input tensao @@;
cards;
16.62 16.75 17.37 17.12 16.98
16.87 17.34 17.02 17.08 17.27
;
run;
proc iml;
reset print;
use tensao1;
read all into y1;
use tensao2;
read all into y2;
n1=nrow(y1); n2=nrow(y2);
y1_2=y1#y1;
y2_2=y2#y2;
s1=sum(y1_2)/n1-(sum(y1)/n1)**2;
s2=sum(y2_2)/n2-(sum(y2)/n2)**2;
sp=((n1-1)*s1+(n2-1)*s2)/(n1+n2-2);
est_t=((sum(y1)/n1)-(sum(y2)/n2))/(sp*sqrt(1/n1+1/n2));
p_valor=probt(est_t,n1+n2-2);
s1=var(y1);
y=y1//y2;

here I wanna change the order of the numbers and make another matrix y... So i'll have another matrix y1 and y2

y_1=y[1:n1,];
y_2=y[n1+1:n1+n2];


Anyway... sorry about the mess... After I discover it I'll redesign it! =P
1 REPLY 1
Rick_SAS
SAS Super FREQ
You need to generate a permutation of the numbers 1:N (N=number of obs) and use the permutation as indices to form a new vector.
There are several ways to do this. The simplest is to generate random uniform numbers and then use the RANK function to assign integers that correspond to the order of the random numbers:

N = 10; /* number of observations in data */
u = j(N, 1); /* allocate Nx1 vector */
call randgen(u, "Uniform"); /* fill u with random uniform */
p = rank(u); /* p is random permutation of 1:N */
print u p;

newY = oldY

; /* new vector = permutation of data */

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