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

Hi,

I am struggling to generate different groups in proc means. I am trying to generate weekly average temperature groups using id variable and IN operator, and then get their average of each group.

I developed the following syntax, but something is wrong. Could someone help me find what the mistake is?

thank you.

 

This is my syntax :

 

proc means data= id80_00_20a;
where id (in (1,2,3,4,5,6, 7))=1;
where id (in (8,9,10,11,12,13,14, 15))=2 ;
where id (in (16,17,18,19,20,21,22,23))=3;
where id (in (24,25,26,27,28,29,30, 31))=4;
class id;
var avg1 avg2 avg3 ;
run;

 

 

This is what the log page says:

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc means data= id80_00_20a;
74 where id (in (1,2,3,4,5,6, 7))=1;
ERROR: Function IN requires a character expression as argument 1.
75 where id (in (8,9,10,11,12,13,14, 15))=2 ;
ERROR: Function IN requires a character expression as argument 1.
76 where id (in (16,17,18,19,20,21,22,23))=3;
ERROR: Function IN requires a character expression as argument 1.
77 where id (in (24,25,26,27,28,29,30, 31))=4;
ERROR: Function IN requires a character expression as argument 1.
78 class id;
79 var avg1 avg2 avg3 ;
80 run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The syntax for IN is 

value in (...list...)

and it returns a boolean results.

 

Why not make a format?

proc format ;
value idgroup
 1-7='Group 1'
 8-15='Group 2'
16-23='Group 3'
24-31='Group 4'
;
run;

proc means data= id80_00_20a;
  class id;
  var avg1 avg2 avg3 ;
  format id idgrp.;
run;

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

The syntax for IN is 

value in (...list...)

and it returns a boolean results.

 

Why not make a format?

proc format ;
value idgroup
 1-7='Group 1'
 8-15='Group 2'
16-23='Group 3'
24-31='Group 4'
;
run;

proc means data= id80_00_20a;
  class id;
  var avg1 avg2 avg3 ;
  format id idgrp.;
run;

 

ChrisNZ
Tourmaline | Level 20

1.  A where clause is used to subset a table.

Here you have 4 clauses, so the last one supersedes the others.

 

2.  It seems that you want to change the value of ID.

A where clause does not do this.

 

3. The syntax you use is invalid. This syntax is valid:

where ID in (1,2,3,4,5,6,7) ;

 

 

 

ballardw
Super User

@xoxozav_1 wrote:

Hi,

I am struggling to generate different groups in proc means. I am trying to generate weekly average temperature groups using id variable and IN operator, and then get their average of each group.

I developed the following syntax, but something is wrong. Could someone help me find what the mistake is?

thank you.

 

This is my syntax :

 

proc means data= id80_00_20a;
where id (in (1,2,3,4,5,6, 7))=1;
where id (in (8,9,10,11,12,13,14, 15))=2 ;
where id (in (16,17,18,19,20,21,22,23))=3;
where id (in (24,25,26,27,28,29,30, 31))=4;
class id;
var avg1 avg2 avg3 ;
run;

 

 

This is what the log page says:

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 proc means data= id80_00_20a;
74 where id (in (1,2,3,4,5,6, 7))=1;
ERROR: Function IN requires a character expression as argument 1.
75 where id (in (8,9,10,11,12,13,14, 15))=2 ;
ERROR: Function IN requires a character expression as argument 1.
76 where id (in (16,17,18,19,20,21,22,23))=3;
ERROR: Function IN requires a character expression as argument 1.
77 where id (in (24,25,26,27,28,29,30, 31))=4;
ERROR: Function IN requires a character expression as argument 1.
78 class id;
79 var avg1 avg2 avg3 ;
80 run;

If by any chance you had an actual date value for each record you could use a FORMAT to get values by week, depending on your "week" definiton. SAS uses 3 different appearances look at WEEKU WEEKV and WEEKW for the differences in starting day of the week and cross year behavior. This shows data that crosses a year boundary with a 2 digit year and the week indicator.

data have;
   do date='14Jul2018'd to '05Mar2019'd;
      randomvalue = 100*(rand('uniform'));
      output;
   end;
run;

proc summary data=have nway ;
   class date;
   format date weekv5.;
   var randomvalue;
   output out=work.summary mean=;
run;

You could create a nicer format for appearance using Proc Format.

An advantage of the DATE plus format is that you can create other summary groups just changing the format. YYMM would give a year/month summary, YYQ would give a year/quarter summary, YEAR would give a calendar year summary, MONTH would give a calendar month summary across years and more with other formats.

 

Or starting with a date you could create custome intervals with the INTNX function to create a date representative of LOTS of things.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 498 views
  • 1 like
  • 4 in conversation