BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
paddyb
Quartz | Level 8

data have;

input @1 subject 3.

@4 code $1.

@6 result 3.

;

datalines;
101 A 120
101 A 130
101 B 129
102 C 45
102 C 124
103 D 222
103 D 234
103 E 123
103 E 123

;

run;

 

expected ouput i want like this where i am adding new obs for average of each unique subject and code.I tried using first.subject and first.code but its still retaining previous avg value in new subject.Can anyone help how to achieve it.

Thanks.


subject code result average
101       A     120
101       A     130
101                        125
101       B     129
101                        129
102      C      45
102      C     124
102                        84.5
103      D      222
103      D      234
103                        228
103      E      123
103      E      123
103                        123

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

proc sort data=have;

by subject code;

run;

 

data want;

set have;

by subject code;

if first.code then do;

n=0;

temp=0;

end;

N+1;

temp+result;

output;

if last.code then do;

average=temp/n;

output;

end;

drop n temp;

run;

 

Regards,

Naveen Srinivasan

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20

proc sort data=have;

by subject code;

run;

 

data want;

set have;

by subject code;

if first.code then do;

n=0;

temp=0;

end;

N+1;

temp+result;

output;

if last.code then do;

average=temp/n;

output;

end;

drop n temp;

run;

 

Regards,

Naveen Srinivasan

paddyb
Quartz | Level 8

Thanks a lot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 727 views
  • 0 likes
  • 2 in conversation