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


Hi Team,

Desc            stay

Hosp            32

Hosp            20

Hosp            15

apartment       5 

vacation       20

vacation       12

vacation       5

vacation      10

If is the first of the case i want to create stay1 variable and say stay1=stay

elso do stay1=the avarage of stay

Output is like

Desc            stay1  

Hosp            22.33

apartment       5 

vacation         11.75

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Normally to summarize you would use PROC SUMMARY.

proc summary data=have nway missing;

  class desc ;

  var stay1 ;

  output out=want mean=stay1;

run;

You could do it with SQL

proc sql ;

  create table want as

    select desc,mean(stay) as stay1

    from have

    group by 1

  ;

quit;

You could even do it with a data step, but why would you?

data want ;

  do _n_=1 by 1 until (last.desc);

    set have;

    by desc;

    stay1 = sum(stay,stay1);

  end;

  stay1=stay1/_n_;

run;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Normally to summarize you would use PROC SUMMARY.

proc summary data=have nway missing;

  class desc ;

  var stay1 ;

  output out=want mean=stay1;

run;

You could do it with SQL

proc sql ;

  create table want as

    select desc,mean(stay) as stay1

    from have

    group by 1

  ;

quit;

You could even do it with a data step, but why would you?

data want ;

  do _n_=1 by 1 until (last.desc);

    set have;

    by desc;

    stay1 = sum(stay,stay1);

  end;

  stay1=stay1/_n_;

run;

art297
Opal | Level 21

: The definition of a mean is the sum of values divided by the number of values thus, in this case, the sum of all 10 values divided by 10.  Since there will likely be more codes that you have to group together, it will probably be easiest to create a format that reflects the necessary groupings and incorporate the format into your analysis.

newbie_ari
Fluorite | Level 6

As Tom suggested you can do it using "Proc Summary". You can also do using "Proc Means":

proc means data=test mean;

class desc;

var stay;

OUTPUT OUT=avg;

run;

robertrao
Quartz | Level 8

Hi,

In the SQL method was does the :

GROUP BY 1 mean????

I GET THE ANSWER BUT WOULD IT NOT BE "GROUP BY DESC?????

ALSO what does:

desc,mean(stay) mean????

Regards

art297
Opal | Level 21

First question: You can identify variables by the position number (left to right) from the order they are listed on the select statement.  Thus, since desc was listed first, it can be specified as 1.  That is sometimes important because a new variable might be computed in the select statement, but not explicitly assigned a name.

Second question: desc and stay are the names of your two variables.  Thus it is selecting desc and the mean of stay.  Tom's suggested code actually specified select desc,mean(stay) mean as stay1, which was saying select desc and the mean of stay, and assign the mean to a variable called stay1.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 1665 views
  • 8 likes
  • 4 in conversation