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;

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
  • 5 replies
  • 1291 views
  • 1 like
  • 3 in conversation