BookmarkSubscribeRSS Feed
chriswong1386
Calcite | Level 5

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:

nameyesnosum of yessum of no
chris 4214060
ken3111030
tom137060

 

 

please help!

I just know need to proc sort it first by name. I don't know how to collapse it afterwards 

6 REPLIES 6
chriswong1386
Calcite | Level 5
proc freq is not allow, it need to be done within a data step, but can assume the data about is already create and can use
data answer;
set try;
...
andreas_lds
Jade | Level 19

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

 

chriswong1386
Calcite | Level 5

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

1chrisyes6064200
2chrisno502260
3chrisno101110
4chrisyes103170
5chrisyes4053140
6chrisyes3042100
7kenno3011230
8kenyes1043340
9kenyes4032330
10kenyes6021290
11tomyes7041470
12tomno3033400
13tomno2022370
14tomno1011350

 

1chrisno502260
2chrisyes6064200
3kenno3011230
4kenyes1043340
5tomno3033400
6tomyes7041470

 

really stuck, nowhere near the answer, please help me!  

 

 

chriswong1386
Calcite | Level 5

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;

 

1chrisno502260 
2chrisyes6064200 
3kenno3011230 
4kenyes1043340 
5tomno3033400 
6tomyes7041470

 

 

help !, I am stuck! still no where near!

 

AndreaVianello
Obsidian | Level 7
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
; run;

proc sql;
select name , sum(yesno='yes') as yes, sum(yesno='no') as no ,
sum( price * (yesno='yes')) as sum_of_yes, sum( price * (yesno='no')) as sum_of_no
from try
group by name
; quit;

Ksharp
Super User
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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 718 views
  • 3 likes
  • 4 in conversation