Obsidian | Level 7

## simplify do loops

my code (that works):

``````data home.test2; set bv18u; array values (*) COGS--EBITDA; do i = 1 to dim(values);
if nmonths=1 then values(i) = 12/1*values(i); else if Number_of_months=2 then values(i) = 12/2*values(i);
else if nmonths=3 then values(i) = 12/3*values(i); else if nmonths=4 then values(i) = 12/4*values(i);
else if nmonths=5 then values(i) = 12/5*values(i); else if nmonths=6 then values(i) = 12/6*values(i);
else if nmonths=7 then values(i) = 12/7*values(i); else if nmonths=8 then values(i) = 12/8*values(i);
else if nmonths=9 then values(i) = 12/9*values(i); else if nmonths=10 then values(i) = 12/10*values(i);
else if nmonths=11 then values(i) = 12/11*values(i); else if nmonths=12 then values(i) = 12/12*values(i);
else if nmonths=13 then values(i) = 12/13*values(i); else if nmonths=14 then values(i) = 12/14*values(i);
else if nmonths=15 then values(i) = 12/15*values(i); else if nmonths=16 then values(i) = 12/16*values(i);
else if nmonths=17 then values(i) = 12/17*values(i); else if nmonths=18 then values(i) = 12/18*values(i);
else if nmonths=19 then values(i) = 12/19*values(i); else if nmonths=20 then values(i) = 12/20*values(i);
else if nmonths=21 then values(i) = 12/21*values(i); else if nmonths=22 then values(i) = 12/22*values(i);
else if nmonths=23 then values(i) = 12/23*values(i); else if nmonths=24 then values(i) = 12/24*values(i);
end;
``````

to simplify this code I tried doing (that doesn't work):

``````data home.test1; set bv18u; array values (*) COGS--EBITDA; do i = 1 to dim(values);
do j=1 to 24; if nmonths=j then 12/j*values(i); end; end;``````

and I got an error. log:

``````

733        data bvd.test1; set bv18u; array values (*) COGS--EBITDA; do i = 1 to dim(values);
734        	do j=1 to 24; if nmonths=j then 12/j*values(i); end; end;
__
180
ERROR 180-322: Statement is not valid or it is used out of proper order.``````

1 ACCEPTED SOLUTION

Accepted Solutions
Barite | Level 11

## Re: simplify do loops

Hi @Satori

``12/j*values(i)``

is not a complete statement. You will need something like

``x=12/j*values(i)``

``values(i)=12/j*values(i)``

(I just looked at the one syntax part)

Best Markus

Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
2 REPLIES 2
Barite | Level 11

## Re: simplify do loops

Hi @Satori

``12/j*values(i)``

is not a complete statement. You will need something like

``x=12/j*values(i)``

``values(i)=12/j*values(i)``

(I just looked at the one syntax part)

Best Markus

Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
Super User

## Re: simplify do loops

You need a complete assignment statement, and can omit the DO:

``````data home.test1;
set bv18u;
array values (*) COGS--EBITDA;
do i = 1 to dim(values);
if 1 le nmonths le 24 then values{i} = 12 / nmonths * values{i};
end;
run;``````
Discussion stats
• 2 replies
• 234 views
• 1 like
• 3 in conversation