Help using Base SAS procedures

Mean of all observation in a separate column

Accepted Solution Solved
Reply
Super Contributor
Posts: 401
Accepted Solution

Mean of all observation in a separate column

Hi, is there a way I can calculate the mean for a column and display it consistently throughout each observation.. here's what I mean

HAVE:

Var1     Var2

DE          3

LM          .

SE          5

WANT:

Var1     Var2     MEAN

DE          3              4

LM          .               4

SE          5              4

Thanks


Accepted Solutions
Solution
‎06-15-2012 11:31 AM
Super Contributor
Posts: 1,636

Re: Mean of all observation in a separate column

How about:

data have;

infile cards missover;

input Var1 :$     Var2;

cards;

DE          3

LM          .

SE          5

;

proc sql;

  create table want as

    select *,mean(var2) as mean

   from have;

quit;

proc print;run;

Linlin

View solution in original post


All Replies
Solution
‎06-15-2012 11:31 AM
Super Contributor
Posts: 1,636

Re: Mean of all observation in a separate column

How about:

data have;

infile cards missover;

input Var1 :$     Var2;

cards;

DE          3

LM          .

SE          5

;

proc sql;

  create table want as

    select *,mean(var2) as mean

   from have;

quit;

proc print;run;

Linlin

Super Contributor
Posts: 401

Re: Mean of all observation in a separate column

Thanks Linlin, it works perfectly

Super Contributor
Posts: 1,636

Re: Mean of all observation in a separate column

then should you mark my answer as correct answer instead of yours?Smiley Wink

Super Contributor
Posts: 401

Re: Mean of all observation in a separate column

I'm not so savy in proc sql, but how could I create anew field in the same procedure that calculates the difference between Mean and Var2 ?  Thanks

Super Contributor
Posts: 1,636

Re: Mean of all observation in a separate column

Yes,sure.

data have;

infile cards missover;

input Var1 :$     Var2;

cards;

DE          3

LM          .

SE          5

;

proc sql;

  create table want as

    select *,mean(var2) as mean,calculated mean-var2 as diff

   from have;

quit;

proc print;run;

Linlin

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 3008 views
  • 0 likes
  • 2 in conversation