## SAS array addition // FOR EACH?

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 2000001 02 2350001 03 2102002 01 2366402 02 1542002 03 1420003 01 2562303 02 1250003 03 20030;run; `

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

PROC Star
Posts: 1,283

## 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
Posts: 8,116

## 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
Posts: 9,599

## 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: 23,754

## 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.

Discussion stats
• 4 replies
• 202 views
• 6 likes
• 5 in conversation