BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ManitobaMoose
Quartz | Level 8
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.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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 ; 

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
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 ; 
ManitobaMoose
Quartz | Level 8

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!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1204 views
  • 0 likes
  • 2 in conversation