New Contributor
Posts: 3

# Retain and Do Loops

I was able to get some help and get this far:

data want;
do year = &FirstYear to &CurrentYr;
output;
end;
retain m_03 0;
retain m_06 0;
retain m_09 0;
run;

However, let's say I want my table to go to m_225, how would I do that?

I thought I could use a do loop but it ended up giving me a weird table with the variable I used for my counter as a table column.

Thanks.

Super User
Posts: 9,407

## Re: Retain and Do Loops

??

Sorry your post makes no sense.  Do you want new variables which have m_xyz up to 225?  If so then an array, however there would be better modelling techniques than 225 variables.

The retain seems to be a bit pointless, just creating 3 variables with the same data.  And the do just creates a bunch of rows.  Not sure what you expect us to do with this.

Post test data in the form of a datastep

Show what the output should look like

Explain what the logic between the two is.

Posts: 2,812

## Re: Retain and Do Loops

You've got macro variables (&FirstYear and &CurrentYr) that don't seem to relate to anything else in the program. You've got a do loop that doesn't seem to relate to the retain statements. In short, we don't know what you are trying to do, and the code you have provided doesn't seem to make any sense. You tried something with this code (or was it some other code?) and the result was "weird", but we have no idea what that means either.

So, please explain what you would like to do in words, and not in code. We need a clear and complete explanation, written so that your high school English teacher would give you a grade of A+. And then, we can probably provide code that will get the job done.

--
Paige Miller
Super User
Posts: 13,304

## Re: Retain and Do Loops

`array m_{255}  (255*0);`

If the variables do not exist an array statement such as this will make 255 variables, m_1 to m_255. The (255*0) is not multiplication but is the instruction to set all 255 varaibles to the value of 0 when the array is populated.

Still no example of how this data is to be used. Creating that many zero valued variables often indicates that another approach may well be indicated.

Super User
Posts: 6,629

## Re: Retain and Do Loops

You can see a follow-up post to your original, with a good solution for this:

https://communities.sas.com/t5/Base-SAS-Programming/Create-table-with-incremental-rows-and-years-for...

Discussion stats
• 4 replies
• 89 views
• 0 likes
• 5 in conversation