## Do Loop A liitle bit of assistance

Solved
Frequent Contributor
Posts: 130

# Do Loop A liitle bit of assistance

```Libname Review'/folders/myfolders/Review' ;
Libname Learn'/folders/myfolders/Learn' ;
Libname myformat'/folders/myfolders/sasuser.v94' ;
Options fmtsearch=(myformat) ;

Data review.monthsales ;
input month sales @@ ;
sumsales + sales ;
datalines ;
1 4000 2 5000 3 . 4 5500 5 5000 6 6000 7 6500 8 4500 9 5100 19 5700 11 6500 12 7500
;
run ;

Data review.SalestoDate ;
Set review.monthsales ;
TotalSales = 0 ;
Do Month = 1 to 12 ;
TotalSales = TotalSales + Sales ;
End ;
run ;

proc print data=review.SalestoDate noobs ;
run ; ```

I have figures out how to add up sales using the sum sales statement, and it works. But, I want to be able to achieve the same thing with a do loop. However, I am getting very strange results. Month is 13 for all of the do loop, and the Totalsales, which should match sumsales, start at 48000 and go up. Any help would be greatly appreciated.

Accepted Solutions
Solution
‎04-18-2018 01:36 PM
Super User
Posts: 2,049

## Re: Do Loop A liitle bit of assistance

``````Data monthsales ;
input month sales @@ ;
sumsales + sales ;
datalines ;
1 4000 2 5000 3 . 4 5500 5 5000 6 6000 7 6500 8 4500 9 5100 10 5700 11 6500 12 7500
;
run ;

Data SalestoDate ;
do until(last);
set monthsales end=last;
TotalSales + Sales ;
output;
End ;
run ; ``````

All Replies
Solution
‎04-18-2018 01:36 PM
Super User
Posts: 2,049

## Re: Do Loop A liitle bit of assistance

``````Data monthsales ;
input month sales @@ ;
sumsales + sales ;
datalines ;
1 4000 2 5000 3 . 4 5500 5 5000 6 6000 7 6500 8 4500 9 5100 10 5700 11 6500 12 7500
;
run ;

Data SalestoDate ;
do until(last);
set monthsales end=last;
TotalSales + Sales ;
output;
End ;
run ; ``````
Frequent Contributor
Posts: 130

## Re: Do Loop A liitle bit of assistance

Thanks.After reviewing your code, I see that It turns out that the key problem is that you cannot write Totalsales = Totalsales + Sales if there are any missing values for sales. Instead, Totalsales + Sales.

This code also works:

```Data review.SalestoDate ;
Do Month = 1 to 12 ;
Set review.monthsales ;
TotalSales + Sales ;
Output ;
End ;
run ```