BookmarkSubscribeRSS Feed
buffalol
Fluorite | Level 6

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. 

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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;
Tom
Super User Tom
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Reeza
Super User

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. 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 894 views
  • 6 likes
  • 5 in conversation