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.
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;
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;
This works, brilliant! Thanks so much!
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.