BookmarkSubscribeRSS Feed
skjhzzang
Calcite | Level 5

For example below

 

Peter  2  14  16

James  3  17  19  18

Max  1  7

Logan  5  19  15  11  9  18

 

 

First variable is Name, Second variable is number of visit and others are amounts.

Peter visited twice so there are 2 amount variables and James visited three times so there are 3 amount variables.

Is it possible to just ignore reading Amount variables and just calculate sum of amount variables for each person and running total of the total amount variables for each person in a single data step? 

Result should be like this.

Name   TotalAmt  Runtotal

Peter    30              30

James  54              84

Max      7                91

Logan  72               163

...

...

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

assuming you have variables named AMOUNT1-AMOUNT5

 

Use

 

totalamt=sum(of amount1-amount5);
runtotal+totalamt;
--
Paige Miller
skjhzzang
Calcite | Level 5

@PaigeMiller 

 

What if you don't know how many variables are in the data?

I mean in the datalines, there are random number of amount variables. (You don't know the maximum number of Amount Variable)

Datalines above is just an example in a whole datalines

PaigeMiller
Diamond | Level 26

Surely you must have a SAS data set. PROC CONTENTS will tell you what variables exist, you can use that information.

 

Please show us an actual SAS data set, following these instructions: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 3 replies
  • 493 views
  • 0 likes
  • 2 in conversation