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

A   X  8

A   X  6

A   Y  9

A   Y  2

B   X  7

B   X  5

...

 

How can I calculate a new variable that sum the same A, first X + first Y (8+9), second X + second Y (6+2). results look like this:

A   (8+9=)17

A  (6+2=)8

B  ...

 

Thanks for anyone's help.

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @xmg2088 

 

I am thinking about something like this:

data have;
	input comp $ ind $ value;
	datalines;
A   X  8
A   X  6
A   Y  9
A   Y  2
B   X  7
B   X  5
;
run;

data have_fl;
	set have;
	by comp ind;
	if first.ind then num_obs = 0;
	num_obs + 1;
run;

proc sort data=have_fl;
	by comp num_obs;
run;

proc means data=have_fl sum noprint;
	var value;
	by comp num_obs;
	output out=want (drop=_: num_obs) sum=sum;
run;

proc print;

Capture d’écran 2020-05-03 à 17.45.08.png

View solution in original post

5 REPLIES 5
ed_sas_member
Meteorite | Level 14

Hi @xmg2088 

 

I am thinking about something like this:

data have;
	input comp $ ind $ value;
	datalines;
A   X  8
A   X  6
A   Y  9
A   Y  2
B   X  7
B   X  5
;
run;

data have_fl;
	set have;
	by comp ind;
	if first.ind then num_obs = 0;
	num_obs + 1;
run;

proc sort data=have_fl;
	by comp num_obs;
run;

proc means data=have_fl sum noprint;
	var value;
	by comp num_obs;
	output out=want (drop=_: num_obs) sum=sum;
run;

proc print;

Capture d’écran 2020-05-03 à 17.45.08.png

xmg2088
Calcite | Level 5

This works, brilliant! Thanks so much!

novinosrin
Tourmaline | Level 20


data have;
input comp $ ind $ sales ;
cards;
A   X  8

A   X  6

A   Y  9

A   Y  2

B   X  7

B   X  5
;


data Want ;
 if _n_=1 then do;
  dcl hash H (ordered:'a') ;
  h.definekey  ("_n_") ;
  h.definedata ("sum") ;
  h.definedone () ;
  dcl hiter hi('h');
 end;
 do until(last.comp);
  do _n_=1 by 1 until(last.ind);
   set have;
   by comp ind;
   if h.find() ne 0 then sum=sales;
   else sum=sum(sales,sum);
   h.replace();
  end;
 end;
 do While(hi.next()=0);
   output;
 end;
 h.clear();
 keep comp sum;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 550 views
  • 1 like
  • 3 in conversation