BookmarkSubscribeRSS Feed
asishgautam
Calcite | Level 5

hi,

i have a data set with the following layout:

Zone Building_type Year Day_yr End_use Hour1 Hour2.......Hour24

columns Hour1 Hour2..Hour24 contain electricity use at the given hour.

i would like to calculate the average electricity use. do i have to "nest" the summary function in sql?

proc sql ;

     select Zone,Building_type,Year,mean(mean(Hour1,Hour2,...Hour24))

     from table A

     group by Zone,Building_type,Year ;

quit ;

also, is there a way to specify like named columns in sql? i think in the data step, one could use: mean(of Hour1-Hour24) to calculate the mean for the day.

6 REPLIES 6
art297
Opal | Level 21

One way is to have proc sql create the list by accessing dictionary.columns.  e.g.,

data have;

  set sashelp.class (rename=(

   age=hour1

   height=hour2

   weight=hour3));

run;

proc sql noprint;

  select name into :names

    separated by ","

      from dictionary.columns

        where libname eq "WORK" and

          memname eq "HAVE" and

            upcase(name) like "HOUR%"

  ;

  create table want as

    select name, mean(&names.)

      from have

  ;

quit;

asishgautam
Calcite | Level 5

thanks art! this is a nice work around but it sure would be nice to have a way to reference like named columns directly - similar to in a data step.  on another note, does anyone know of a "list" of data step functions that can not be used in sql? i recall that lag is one function and I believe you can not use arrays or any of the "do loop" constructs.

art297
Opal | Level 21

I don't think I have ever seen such a list.  The documentation states that you can use "most" of the functions but they are not treated as sql aggregate functions.  Take a look at:

http://support.sas.com/documentation/cdl/en/sqlproc/63043/HTML/default/viewer.htm#p0xwc73ekemkapn1by...

FriedEgg
SAS Employee

data want;

set sashelp.class( rename=( age=hour1 height=hour2 weight=hour3 ) );

mean=mean(of hour:);

run;

art297
Opal | Level 21

The OP wanted to do it in proc sql!

FriedEgg
SAS Employee

oh, hah, I somehow missed the idea that it had to be in sql :smileyblush:

variable lists do not work in sql, the best way I know to accomplish the desired outcome it to follow Art's original description.

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!

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.

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
  • 6 replies
  • 970 views
  • 3 likes
  • 3 in conversation