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

 

I have a matrix (let's call it D) with 20 rows (+ column name) and 2 columns. The matrix has been sorted in a particular way, and I want to read the first of every 4 values in the the first column to another matrix. The following code worked on a small "test" dataset (small enough that I could check the results manually), but is there a reason it could produce wrong results (for example, read a number twice from a particular set of four, but zero times from another set)?

 

proc iml;
use sasdata.testnum;
read all var _ALL_ into D[colname = varNames];
close sasdata.testnum;
print D;
 
T = j(5, 1, 0);
 
t_row = 1;
do y = 1 to 21 by 4;
    T[t_row, 1] = D[y, 1];
t_row = t_row + 1;
print T;
 
end;
 

In other words, does anyone see anything wrong with this code? Is there a better way of doing what I have done here?

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Use this. It uses the DO function to generate the sequewnce {1, 5, 9,...}:

 

t_row = do(1, nrow(D), 4);
T = D[t_row, 1];

View solution in original post

2 REPLIES 2
kannand
Lapis Lazuli | Level 10

Have you considered using the var _n_ along with a divisibility function I believe it is called MODZ () ??

Kannan Deivasigamani
Rick_SAS
SAS Super FREQ

Use this. It uses the DO function to generate the sequewnce {1, 5, 9,...}:

 

t_row = do(1, nrow(D), 4);
T = D[t_row, 1];

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 1848 views
  • 0 likes
  • 3 in conversation