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

Hi I hope you're all well,

 

I have a simple problem yet quite disturbing as I don't know what happens exactly and why don't I get the results wanted, to put you in the context let's suppose you have a table with variables like this ;

 

project    company    category    ROI    contribution

 

My aim is quite simple, i wanna sum my contibs according to certain companies by project and category ( i'm sorry i can't give you the right names nor values of my table for confidentiality purposes ).

 

Now since i want only certain clients sums I did this : ( assume the company is Shelbey LTD )

 

proc sort data=table2; by  project category ;run;

options mprint;
data table3;
set table2;
Company = scan(project,1,'_'); /* from here i get my company name */
by project category;
if Company = "SHELBEY LTD" then do;
	if First.contrib then contrib2=0;
		contrib + contrib2;
	if Last.contrib;
end;
run;

Well, in the results all my companies are there with no value in contrib2 ( which is normal considering the fact that i "selected" only the shelbey company ) but all the shelbey observations disappeard !

It's quite weird, this the first time i encounter this kind of difficulty.

 

here is the message i got from the log

NOTE: Variable 'First.contrib'n is uninitialized.
NOTE: Variable 'Last.contrib'n is uninitialized.
NOTE: There were 1052 observations read from the data set WORK.TABLE2.
NOTE: The data set WORK.TABLE3 has 860 observations and 13 variables.
NOTE: Compressing data set WORK.TABLE3 decreased size by 83.33 percent. 
      Compressed is 2 pages; un-compressed would require 12 pages.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

Please Help if you can.

 

Thanks in advance and stay safe 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

I think you should use first.category and last.category instead of first.contrib and last.contrib.

 

I guess you probably also want to change :

contrib+contrib2;

into

contrib2+contrib;

That is contrib2 is the sum of contrib column for the current project/category.

View solution in original post

5 REPLIES 5
gamotte
Rhodochrosite | Level 12
Hello,
You use first.contrib and last.contrib but contrib is not in your by statement.
gamotte
Rhodochrosite | Level 12

I think you should use first.category and last.category instead of first.contrib and last.contrib.

 

I guess you probably also want to change :

contrib+contrib2;

into

contrib2+contrib;

That is contrib2 is the sum of contrib column for the current project/category.

skavli
Calcite | Level 5
why is the second order important ?
gamotte
Rhodochrosite | Level 12
COL1+COL2;

is a short way to write :

retain COL1;

COL1=sum(COL1, COL2);

So the leftmost operand should be the name of the column that will contain the summation result.

skavli
Calcite | Level 5
Oh yes, I realized that i have loop over the occurences in my by group not the values that corresponds to it thanks !

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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