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.
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 ;
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 ;
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
Appreciate your help!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.