BookmarkSubscribeRSS Feed
bigbigben
Obsidian | Level 7

It is an easy job if the gap between the first number and the last is a multiple of the step size; however, I want to do it in more general cases.

For example, if I want to generate a seris starting from 1 until 45 while the value increases by 4 each time.

In matlab, we can type 1:4:45 and it gives [1 5 9 ... 41 45];  If I write 2:4:45, it will give [2 6 10 ... 42].

I know I can write a loop in SAS, but it could be increase the computation time dramatically. Is there an easier way to do it?

Thanks.

4 REPLIES 4
Rick_SAS
SAS Super FREQ

The matlab syntax is NSTART: INCR: NSTOP. You can do the same in SAS/IML by using the DO function:

x = do( nstart, nstop, incr);

For example,

x = do( 1, 45, 4);

y = do( 2, 45, 4);

For more on this topic (including the SAS/IML equivalent of the linspace() function in MATLAB), see http://blogs.sas.com/content/iml/2011/01/10/creating-vectors-that-contain-evenly-spaced-values/

If you need to generate points on a 2D grid, see http://blogs.sas.com/content/iml/2011/01/21/how-to-create-a-grid-of-values/

TomTom
Calcite | Level 5

PROC IML;

    i = DO(2,45,4);

    PRINT i;

QUIT;

All of the questions you are asking on the SAS/IML forum are rather basic. May I suggest that you buy (or borrow) Rick's book to properly learn IML from scratch?

Rick_SAS
SAS Super FREQ

Questions at all levels are welcome. It is often difficult for a beginner to get started, especially when you are coming from another language like MATLAB.

TomTom correct mentions that there are some resources that might help you with the learning curve:

- the SAS/IML Tip sheet

- a short (and not complete) "MATLAB to SAS/IML Tip Sheet"

- the FREE chapter of my book [HIGHLY RECOMMENDED]

- Read the article "How to learn SAS/IML: Five resources for the beginner"

In the "How to learn SAS/IML" article, the 3rd suggestion is "ask questions at the SAS/IML discussion forum"! So (IMHO), feel free to ask for help when you get stuck.

bigbigben
Obsidian | Level 7

Rick and TomTom. Many thanks for your response and help. I used Matlab pretty often before, and there are tons of sample codes of user-defined functions online that I can borrow or use as references. It is not very easy to find such resources in SAS. As my defense, even I read Rick's book, I probably don't remember all the functions used in SAS/IML. That being said, I will read all the documents recommended by both of you.

Best wishes.

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
  • 4 replies
  • 1754 views
  • 0 likes
  • 3 in conversation