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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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