BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mrdlau
Obsidian | Level 7

suppose I have 5 columns in my output:

c1 c2 c3 c4 c5

1   2   3   4  5

 

how do I output my result so that it displays a cumulative sum from left to right

 

c1 c2 c3 c4 c5

1  3  6  10  15

 

In this example,  I have c1-c5, but in reality,  that may not be the case.  i may be c1-c50, or c1-c60

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Easiest is likely to create a new data set:

data want;
   set have;
   array cm c1-c5;
   do i= 2 to dim(cm);
      cm[i] = sum(cm[i-1],cm[i]);
   end;
run;

If your variables are actually named in sequence then it is real easy to use the variable list as above to reference them in order.

If the names are not like that then you need to list them explicitly in the order you want the accumulation to happen.

View solution in original post

3 REPLIES 3
ballardw
Super User

Easiest is likely to create a new data set:

data want;
   set have;
   array cm c1-c5;
   do i= 2 to dim(cm);
      cm[i] = sum(cm[i-1],cm[i]);
   end;
run;

If your variables are actually named in sequence then it is real easy to use the variable list as above to reference them in order.

If the names are not like that then you need to list them explicitly in the order you want the accumulation to happen.

mrdlau
Obsidian | Level 7
Thanks! I think this worked perfectly.....
Can you explain what 'cm' is?
The code worked perfectly but i still want to know what cm is?
I'm a bit new to SAS
ballardw
Super User

@mrdlau wrote:
Thanks! I think this worked perfectly.....
Can you explain what 'cm' is?
The code worked perfectly but i still want to know what cm is?
I'm a bit new to SAS

CM was the name of the array used. It could be any valid SAS variable name that is not already in the data.

Since arrays are just a shorthand method of addressing each of the variables in a list I tend to use a short name that may have some element in common with the names. In this case it was short for "cumulative" which could be used as a valid name but unless there is an overriding need for verbosity I don't like to use such for array names as they do not exist after the data step runs.

 

You will find that any time you want to do something identical or similar to many variables that an array and the shorthand is much easier than writing the same lines of code over and over again for each variable.

 

The special function DIM, which only works with arrays, is used to report on the number of elements defined. So if your actual data has 50 "c" variables then you just change the list of variables to include c50 instead of c5 and the DIM function adjusts to use the longer list.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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