Statistical programming, matrix languages, and more

Generating numbers with given stepsize and range

Posts: 35

Generating numbers with given stepsize and range

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?


Posts: 3,222

Generating numbers with given stepsize and range

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

If you need to generate points on a 2D grid, see

Occasional Contributor
Posts: 17

Generating numbers with given stepsize and range


    i = DO(2,45,4);

    PRINT i;


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?

Posts: 3,222

Re: Generating numbers with given stepsize and range

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.

Posts: 35

Re: Generating numbers with given stepsize and range

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.

Post a Question
Discussion Stats
  • 4 replies
  • 3 in conversation