BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
asinusdk
Calcite | Level 5
data have
input year id v1 v2 v3;
cards;
1 1 . 1 20
1 1 . 1 30
1 1 . 1 10
2 1 . 1 10
3 1 . 1 10
3 1 . 1 20
1 2 . 1 20
1 2 . 2 30
2 2 . 1 20
2 2 . 1 20
;

From this data,

I want to summarize the number by id and year. What I want is... 

data want;
input year id v1_sum v2_sum v3_sum;
cards;
1 1 . 3 60
2 1 . 1 10
3 1 . 2 30
1 2 . 3 50 
2 2 . 2 40
3 2 . . .
; 

The code I tried is something like this:

 

proc sql;
create table sum_diag as
select person_id, year,
from diagnos
group by year
order by person_id, year; quit;

 

I couldn't get the result by year. There were multiple observations  per year for several rows. What should I do to get sum by year within a subject? 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
proc sql;
create table sum_diag as
select id, year, sum(v1) as v1_sum,sum(v2) as  v2_sum ,sum(v3) as v3_sum
from diagnos
group by id,year
order by id, year; 
quit;

View solution in original post

6 REPLIES 6
Reeza
Super User

Any reason to not use a summary proc designed for analysis?

 

Either way, your code doesn't show any attempt at summarization, did you remove that portion before posting?

Otherwise, adding in your summary stats is all you need.

 

proc sql;
create table sum_diag as
select person_id, year, sum(v1) as sum_v1, sum(v2) as sum_v2
from diagnos
group by year
order by person_id, year; quit;
asinusdk
Calcite | Level 5

I actually tried this code, but the result showed the sum of all v1 frequency from all subjects and all years.

novinosrin
Tourmaline | Level 20
proc sql;
create table sum_diag as
select id, year, sum(v1) as v1_sum,sum(v2) as  v2_sum ,sum(v3) as v3_sum
from diagnos
group by id,year
order by id, year; 
quit;
PGStats
Opal | Level 21

You can include the combinations of year and id that do not occur in your data wit a cross join (cartesian product) and a left join:

 

proc sql;
create table want as
select a.year, b.id,
    sum(v1) as sumV1, sum(v2) as sumV2, sum(v3) as sumV3
from 
    (select unique year from have) as a cross join
    (select unique id from have) as b left join
    have as c on a.year=c.year and b.id=c.id
group by a.year, b.id
order by id, year;
quit;

PG
asinusdk
Calcite | Level 5

ERROR: Column person_id could not be found in the table/view identified with the correlation
name b.
ERROR: Expression using equals (=) has components that are of different data types.

 

Thank you very much...But these errors came up...

Reeza
Super User
Is there a variable named person_id in the table? From your code you referred to it that way, but your example data shows a variable named ID? And do the variables have the same type.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 922 views
  • 0 likes
  • 4 in conversation