data try;
input name $ yesno $ price;
datalines;
chris yes 10
chris no 10
chris yes 30
chris yes 40
chris no 50
chris yes 60
ken yes 60
ken yes 40
ken no 30
ken yes 10
tom no 10
tom no 20
tom no 30
tom yes 70
;
The final result want to be:
name | yes | no | sum of yes | sum of no |
chris | 4 | 2 | 140 | 60 |
ken | 3 | 1 | 110 | 30 |
tom | 1 | 3 | 70 | 60 |
please help!
I just know need to proc sort it first by name. I don't know how to collapse it afterwards
Looks like homework, so i post only some hints until you showed more effort solving this yourself.
- BY statement to tell sas that data should be processed by name, enabling first.name to reset counters and last.name to output the summary observation
- LENGTH statement to define variables for the counters, those should be RETAINed, note: names like "sum of yes" are not allowed, you may want to attach a label
proc sort data=try ;
by name yesno;
data try1;
set try;
by name yesno;
x=first.name;
y=last.name;
if first.name=1 then
b=0;
b+1;
if first.yesno=1 then
c=0;
c+1;
acc_price+price;
/*if last.name=1 or last.yesno=1 then output;*/
drop x y;
name yesno price b c acc_price
1 | chris | yes | 60 | 6 | 4 | 200 |
2 | chris | no | 50 | 2 | 2 | 60 |
3 | chris | no | 10 | 1 | 1 | 10 |
4 | chris | yes | 10 | 3 | 1 | 70 |
5 | chris | yes | 40 | 5 | 3 | 140 |
6 | chris | yes | 30 | 4 | 2 | 100 |
7 | ken | no | 30 | 1 | 1 | 230 |
8 | ken | yes | 10 | 4 | 3 | 340 |
9 | ken | yes | 40 | 3 | 2 | 330 |
10 | ken | yes | 60 | 2 | 1 | 290 |
11 | tom | yes | 70 | 4 | 1 | 470 |
12 | tom | no | 30 | 3 | 3 | 400 |
13 | tom | no | 20 | 2 | 2 | 370 |
14 | tom | no | 10 | 1 | 1 | 350 |
1 | chris | no | 50 | 2 | 2 | 60 |
2 | chris | yes | 60 | 6 | 4 | 200 |
3 | ken | no | 30 | 1 | 1 | 230 |
4 | ken | yes | 10 | 4 | 3 | 340 |
5 | tom | no | 30 | 3 | 3 | 400 |
6 | tom | yes | 70 | 4 | 1 | 470 |
really stuck, nowhere near the answer, please help me!
proc sort data=try ;
by name yesno;
data try1;
set try;
by name yesno;
x=first.name;
y=last.name;
if first.name=1 then
b=0;
b+1;
if first.yesno=1 then
c=0;
c+1;
acc_price+price;
if last.name=1 or last.yesno=1 then output;
drop x y;
1 | chris | no | 50 | 2 | 2 | 60 |
2 | chris | yes | 60 | 6 | 4 | 200 |
3 | ken | no | 30 | 1 | 1 | 230 |
4 | ken | yes | 10 | 4 | 3 | 340 |
5 | tom | no | 30 | 3 | 3 | 400 |
6 | tom | yes | 70 | 4 | 1 | 470 |
help !, I am stuck! still no where near!
data try;
input name $ yesno $ price;
datalines;
chris yes 10
chris no 10
chris yes 30
chris yes 40
chris no 50
chris yes 60
ken yes 60
ken yes 40
ken no 30
ken yes 10
tom no 10
tom no 20
tom no 30
tom yes 70
;
data want;
do until(last.name);
set try;
by name;
length yes no sumofyes sumofno 8 ;
if yesno='yes' then do;yes+1;sumofyes+price;end;
if yesno='no' then do;no+1;sumofno+price;end;
end;
output;
call missing(yes ,no, sumofyes, sumofno );
keep name yes no sumofyes sumofno ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.