DATA Step, Macro, Functions and more

SAS array addition // FOR EACH?

Reply
Occasional Contributor
Posts: 9

SAS array addition // FOR EACH?

I'm putting together a dataset using arrays created from another dataset. Lets say the dataset is as follows:

input shop year sales;
datalines;
01 01 20000
01 02 23500
01 03 21020
02 01 23664
02 02 15420
02 03 14200
03 01 25623
03 02 12500
03 03 20030
;
run;


I want to get the total sales for each shop using an array. 

PROC Star
Posts: 765

Re: SAS array addition // FOR EACH?

Why do you want to use an array to do this? You can do it like this

 

data have;
input shop year sales;
datalines;
01 01 20000
01 02 23500
01 03 21020
02 01 23664
02 02 15420
02 03 14200
03 01 25623
03 02 12500
03 03 20030
;

proc sort data = have;
	by shop;
run;

data want(keep = shop total);
	set have;
	by shop;
	if first.shop then total = 0;
	total + sales;
	if last.shop then output;
run;
Super User
Super User
Posts: 7,076

Re: SAS array addition // FOR EACH?

[ Edited ]

Why would you want to use an array?

To get the total sales just use PROC SUMMARY.

proc summary data=have nway ;
  class shop;
  var sales ;
  output out=want sum=total_sales;
run;
Super User
Super User
Posts: 7,988

Re: SAS array addition // FOR EACH?

Look at proc means or summary with a by line for shop.  This is the purpose of such procedures:

http://www2.sas.com/proceedings/sugi29/240-29.pdf

 

For example:

proc means data=have;
  by shop;
  var sales;
  output out=want sum=sum;
run;
Super User
Posts: 19,867

Re: SAS array addition // FOR EACH?

As others have implied an Array is not the correct method for solving this problem. In SAS, an Array is used on a single row, typically, not across multiple rows. 

 

You can summarize data using a summary PROC, PROC SQL, or a data step using FIRST/LAST. 

Ask a Question
Discussion stats
  • 4 replies
  • 159 views
  • 6 likes
  • 5 in conversation