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.
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;
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;
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.