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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

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

podarum
Quartz | Level 8

Thanks Linlin, it works perfectly

Linlin
Lapis Lazuli | Level 10

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

podarum
Quartz | Level 8

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

Linlin
Lapis Lazuli | Level 10

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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