DATA Step, Macro, Functions and more

Do Loop A liitle bit of assistance

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 119
Accepted Solution

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
PROC Star
Posts: 1,321

Re: Do Loop A liitle bit of assistance

Posted in reply to ManitobaMoose
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


All Replies
Solution
‎04-18-2018 01:36 PM
PROC Star
Posts: 1,321

Re: Do Loop A liitle bit of assistance

Posted in reply to ManitobaMoose
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: 119

Re: Do Loop A liitle bit of assistance

Posted in reply to novinosrin

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!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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