Contributor
Posts: 37

# 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?

Thanks.

SAS Super FREQ
Posts: 4,242

## Generating numbers with given stepsize and range

Posted in reply to bigbigben

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/

Occasional Contributor
Posts: 17

## Generating numbers with given stepsize and range

Posted in reply to bigbigben

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?

SAS Super FREQ
Posts: 4,242

## 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.

Contributor
Posts: 37

## Re: Generating numbers with given stepsize and range

Posted in reply to bigbigben

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.

Discussion stats
• 4 replies
• 432 views
• 0 likes
• 3 in conversation