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

Hi,

My data looks something like this:

input load day month 
datalines:
12345 0 12
12456 1 12
12222 2 12
14532 3 12
15111 4 12
15222 5 12
15333 6 12
;

and so on. I want to group into weekdays and weekends where day (6,0) = weekend and (1,2,3,4,5) = weekday

and then make a scatterplot to compare weekend load vs weekday load

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Close. 

 

You create the weekday variable as indicated by a previous poster. That value is stored in the variable PERIOD. Then you would put that variable name in the GROUP = section. 

 

data have;
input load day month temperature;
weekday_end = ifc(day in (0,6), 'Weekend', 'Weekday', .);
datalines;
12345 0 12 35
12456 1 12 33
12222 2 12 32
14532 3 12 31
15111 4 12 25
15222 5 12 24
15333 6 12 33
;
   proc sgplot data=have;
      title 'Weekend Load vs Weekday Load';
      scatter x=Temperature y=Load / group= weekday_end;
   run;

@matt23 wrote:

Sorry this was a bad data example. Here's better representation

input load day month temperature
datalines:
12345 0 12 35
12456 1 12 33
12222 2 12 32
14532 3 12 31
15111 4 12 25
15222 5 12 24
15333 6 12 33
;

And now I want to do a grouped scatter plot:

   proc sgplot data=dataname;
      title 'Weekend Load vs Weekday Load';
      scatter x=Temperature y=Load / group=?? Weekend or Wekkday ??;
   run;

how would I edit my code so it counts day 0 and 6 as weekend and the rest as weekdays?


 

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

With minimal information to go on:

data want;
  set have;
  length period $20;
  period=ifc(day in (6,0),"Weekend","Weekday");
run;

 

matt23
Quartz | Level 8

Sorry this was a bad data example. Here's better representation

input load day month temperature
datalines:
12345 0 12 35
12456 1 12 33
12222 2 12 32
14532 3 12 31
15111 4 12 25
15222 5 12 24
15333 6 12 33
;

And now I want to do a grouped scatter plot:

   proc sgplot data=dataname;
      title 'Weekend Load vs Weekday Load';
      scatter x=Temperature y=Load / group=?? Weekend or Wekkday ??;
   run;

how would I edit my code so it counts day 0 and 6 as weekend and the rest as weekdays?

ballardw
Super User

@matt23 wrote:

Sorry this was a bad data example. Here's better representation

input load day month temperature
datalines:
12345 0 12 35
12456 1 12 33
12222 2 12 32
14532 3 12 31
15111 4 12 25
15222 5 12 24
15333 6 12 33
;

And now I want to do a grouped scatter plot:

   proc sgplot data=dataname;
      title 'Weekend Load vs Weekday Load';
      scatter x=Temperature y=Load / group=?? Weekend or Wekkday ??;
   run;

how would I edit my code so it counts day 0 and 6 as weekend and the rest as weekdays?


Use @RW9's code to add period (or other likely variable name) and use that variable with the group= option.

Or create a custom format, apply that format to the day variable and use day as the group variable.

Reeza
Super User

Close. 

 

You create the weekday variable as indicated by a previous poster. That value is stored in the variable PERIOD. Then you would put that variable name in the GROUP = section. 

 

data have;
input load day month temperature;
weekday_end = ifc(day in (0,6), 'Weekend', 'Weekday', .);
datalines;
12345 0 12 35
12456 1 12 33
12222 2 12 32
14532 3 12 31
15111 4 12 25
15222 5 12 24
15333 6 12 33
;
   proc sgplot data=have;
      title 'Weekend Load vs Weekday Load';
      scatter x=Temperature y=Load / group= weekday_end;
   run;

@matt23 wrote:

Sorry this was a bad data example. Here's better representation

input load day month temperature
datalines:
12345 0 12 35
12456 1 12 33
12222 2 12 32
14532 3 12 31
15111 4 12 25
15222 5 12 24
15333 6 12 33
;

And now I want to do a grouped scatter plot:

   proc sgplot data=dataname;
      title 'Weekend Load vs Weekday Load';
      scatter x=Temperature y=Load / group=?? Weekend or Wekkday ??;
   run;

how would I edit my code so it counts day 0 and 6 as weekend and the rest as weekdays?


 

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 4 replies
  • 1921 views
  • 6 likes
  • 4 in conversation