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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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
  • 980 views
  • 0 likes
  • 2 in conversation