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