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

Hello, 

 

I am pretty new to SAS and I need help in some basic calculations. I want to calculate the average of a subset of numbers in a column and divide every value of that column by the average.

For example, I have a table 

name  age

julia       45

julia       54

julia       34

julia       45

jason      23

jason     56

julia       23

jason     11

jason     34

julia       22

alec       31

alex       33

matt       28

jason     29

 

I want to calculate the average age of Jason and divide every value of age by the average. What is the easiest way to do this and is this something that can be done with proc sql? Thanks in advance for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

You want to divide everyone's age by Jason's average age?

 

If so, here is one way:

 

data have;
  input name $  age;
  cards;
julia       45
julia       54
julia       34
julia       45
jason      23
jason     56
julia       23
jason     11
jason     34
julia       22
alec       31
alex       33
matt       28
jason     29
;

proc sql noprint;
  select mean(age)
    into :jage
      from have
        where name eq 'jason'
  ;
quit;

data want;
  set have;
  jzcore=age/&jage.;
run;

 

Art, CEO, AnalystFinder.com

 

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

You want to divide everyone's age by Jason's average age?

 

If so, here is one way:

 

data have;
  input name $  age;
  cards;
julia       45
julia       54
julia       34
julia       45
jason      23
jason     56
julia       23
jason     11
jason     34
julia       22
alec       31
alex       33
matt       28
jason     29
;

proc sql noprint;
  select mean(age)
    into :jage
      from have
        where name eq 'jason'
  ;
quit;

data want;
  set have;
  jzcore=age/&jage.;
run;

 

Art, CEO, AnalystFinder.com

 

Reeza
Super User

Your question is ambigous. 

 

Are you calculating an average per person and dividing each persons value divided by their individual average

OR

Are you calculating an average per person and dividing each persons value divided by the total average across all people

OR

By Jason's average as indicated?

 

Post sample data and sample output.

vseshad
Calcite | Level 5

I apologize if it seems ambiguous. I want to divide each person's value by Jason's average. 

ChrisBrooks
Ammonite | Level 13

I think this should give you want you want

 

data have;
  input name $  age;
  cards;
julia       45
julia       54
julia       34
julia       45
jason      23
jason     56
julia       23
jason     11
jason     34
julia       22
alec       31
alex       33
matt       28
jason     29
;
run;

proc sql;
	create table want
	as select
	name,
	age,
	age/mean(age) as score
	from have
	group by name;
quit;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 2893 views
  • 1 like
  • 4 in conversation