BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10
data cars ;
set sashelp.cars (keep=Make Type Invoice
where=(Make in ('Buick' 'SUV'))
				);
run ;

Hello,

 

 

for example in this "cars" dataset, I would like to get a column "invoice_cumul"  that cumul invoice by make, type

thanks in advance for your help

Nasser

1 ACCEPTED SOLUTION

Accepted Solutions
Nasser_DRMCP
Lapis Lazuli | Level 10

I found the solution by doing that

proc sort data=have ; by product month_amo month_prod ; run ;

data want  ;
set have ;
by product month_amo ;
retain  amount_cumul ;
if  first.month_amo then amount_cumul = amount ;
else  amount_cumul + amount ;
run ;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Are you sure this is the data set you want to work with? Make is never equal to "SUV"?

Nasser_DRMCP
Lapis Lazuli | Level 10

hello,

sorry for that. this is better

data cars ;
set sashelp.cars (keep=Make Type Invoice
where=(Make in ('Buick' 'Cadillac'))
				);
run ;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are many examples of this in the SAS docs and around.  For example proc means/summary, sum() in proc sql, retain a sum variable in datastep etc.  It really is too simple a question to provide actual code for, just look at the docs for proc means/summary with a by group.

Nasser_DRMCP
Lapis Lazuli | Level 10

hello RW9,

 

thanks for your respons. but whenever a post a question , it is because i did not found the solve my problem by taking a look in the docs. Here is my dataset "have". I would like to get the amout_cumul for each product/month_amo group , for example the first cumul would be 11 + 12 +13 , the second 21+22+23...

Data have  ;
infile datalines ;
input product ~ $4. month_amo month_prod amount ;
datalines ;
PRD1 201801 201801 11
PRD1 201801 201802 12
PRD1 201801 201803 13	
PRD1 201802 201801 21
PRD1 201802 201802 22
PRD1 201802 201803 23
PRD1 201803 201801 31
PRD1 201803 201802 32
PRD1 201803 201803 33
PRD2 201801 201801 41
PRD2 201801 201802 42	
PRD2 201801 201803 43	
PRD2 201802 201801 51
PRD2 201802 201802 52
PRD2 201802 201803 53
PRD2 201803 201801 61
PRD2 201803 201802 62
PRD2 201803 201803 63
;
Nasser_DRMCP
Lapis Lazuli | Level 10

I found the solution by doing that

proc sort data=have ; by product month_amo month_prod ; run ;

data want  ;
set have ;
by product month_amo ;
retain  amount_cumul ;
if  first.month_amo then amount_cumul = amount ;
else  amount_cumul + amount ;
run ;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

From search;

https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

 

50 responses all with code which does what you want. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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