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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 842 views
  • 0 likes
  • 2 in conversation