BookmarkSubscribeRSS Feed
palolix
Lapis Lazuli | Level 10

Dear SAS Community,

 

I needed to alter the order of the months for the variable 'Harvest' (starting with month 11 instead of 1) for a particular Variety when using proc sgpanel so I did it with proc format. However, once I applied this formatting, it applies for the rest of the varieties too, which I don't want. How can I make sure that this formatting only applies for these varieties (I49L and Hass) but not for others?  I would greatly appreciate your help!

 

proc sort data=one out=one; by Harvest; run;
 
data new;
set one;
where Variety in('I49L','Hass');
if (Harvest eq 11) then tag=1;
else if (Harvest eq 12) then tag=2;
else if (Harvest eq 1) then tag=3;
else if (Harvest eq 2) then tag=4;
else if (Harvest eq 3) then tag=5;
else if (Harvest eq 5) then tag=6;
else tag=.;
run;
 
proc format;
value Harvest 1="Nov"
2="Dec"
3="Jan"
4="Feb"
5="Mar"
6="May";
quit;
 
proc sgpanel data=new;
where Season in(1,2,4) and Harvest in(11,12,1,2,3,5) and Variety in('I49L','Hass');
panelby Season/ onepanel spacing=5 novarname;
styleattrs DATACONTRASTCOLORS=(BLUE red);
format tag Harvest.;
label tag="Harvest";
vline tag /lineattrs=(pattern=solid thickness=2) response=DTR group=Variety markers stat=mean ;
rowaxis values=(0 to 12 by 2) ;
run;
12 REPLIES 12
Tom
Super User Tom
Super User

Not sure what you mean.

Could you share a picture of the result and explain what part is different than what you want?

Could you share the data you use?  Preferable as small as necessary to show the issue. 

palolix
Lapis Lazuli | Level 10

Thank you for your quick reply Tom!

 

I want the variety I49L to start with Harvest month 11 (Nov) in the graph, so I did modified that with proc format, but if I want the next variety (GEM) to start with Harvest month 1 (Jan) to 8, then SAS keeps using the formatting from the previous variety (I49L), as you can see in the graph. I hope that helps.

 

proc sgpanel data=GEM;
where Harvest in(1,3,4,5,6,8) and Variety in('GEM','Hass');
panelby Harvest/ onepanel spacing=5 novarname;
styleattrs DATACONTRASTCOLORS=(BLUE red);
vline Wks/lineattrs=(pattern=solid thickness=2) response=DTR group=Variety grouporder=data markers stat=mean ;
rowaxis values=(0 to 20 by 2) ;
run;

 

palolix_0-1777680040676.png

 

Tom
Super User Tom
Super User

The format is applied to the values of the variable it is attached to.  The values of other variables has no impact.

 

If you want the format to display the same values then make sure to code the VARIABLE with the same values.

 

For example you might test the values of the other variable when making decisions about how to assign the values.

data new;
  set one;
  where Variety in('I49L','Hass');
  if Variety='I49L' then do;
    if (Harvest eq 11) then tag=1;
    else if (Harvest eq 12) then tag=2;
    else if (Harvest eq 1) then tag=3;
    else if (Harvest eq 2) then tag=4;
    else if (Harvest eq 3) then tag=5;
    else if (Harvest eq 5) then tag=6;
    else tag=.;
  end;
  else do;
* Put code to create proper values of TAG for the 'Hass' variety here ;
  end;
run;

 

 

Tom
Super User Tom
Super User

Please be more detailed about what you are doing.

 

What are the variable names and types? What do the values mean? What role do they play in the graph you want to create?

 

Based on the code and the picture my GUESS is that in the dataset GEM you have a format attached to the numeric variable HARVEST that converts the values 1 to 'Nov', 3 to 'Jan' , ... 6 to 'May' and 8 to '8'.  I make this guess because in the WHERE statement you limit the set of observations to the ones with those values of HARVEST.  If you would like to make this graph using a different format for HARVEST then add a FORMAT statement to the graph.  If you would like to simply remove the format and have the raw values displayed then do not include a format specification in the FORMAT statement.

format harvest ;

But that is just a guess.

palolix
Lapis Lazuli | Level 10

Thank you so much for your reply Tom. 

I think the problem I am having is due to the format. 

This is the first format I have in my file, which applies to all the varieties I have.

 

proc format;
value Harvest 1 = 'Jan' 2 = 'Feb' 3 = 'Mar' 4 = 'Apr' 5 = 'May' 6 = 'Jun' 7 = 'Jul' 8 = 'Aug' 9 = 'Sept' 10 = 'Oct' 11 = 'Nov' 12 = 'Dec';
run;

 

The thing is that when doing the graphs for the variety 'I49L', I want the sequence of the variable Harvest to be 11(Nov), 12(Dec), 1(Jan), 2(Feb), 3(Mar), 5(May), so if I use the following code I can get that, as you can see in the graph.

 

proc sort data=one out=one; by Harvest; run;
 
data one22;
set one;
 if Variety in('I49L','Hass') then do;
if (Harvest eq 11) then tag=1;
else if (Harvest eq 12) then tag=2;
else if (Harvest eq 1) then tag=3;
else if (Harvest eq 2) then tag=4;
else if (Harvest eq 3) then tag=5;
else if (Harvest eq 5) then tag=6;
else tag=.;
 end;
run;
 
proc format;
value Harvest 1="Nov"
2="Dec"
3="Jan"
4="Feb"
5="Mar"
6="May";
run;
 

proc sgpanel data=one22;
where Season in(1,2,4) and Harvest in(11,12,1,2,3,5) and Variety in('I49L','Hass');
panelby Season/ onepanel spacing=5 novarname;
styleattrs DATACONTRASTCOLORS=(BLUE red);
format tag Harvest.;
label tag="Harvest";
vline tag /lineattrs=(pattern=solid thickness=2) response=DTR group=Variety markers stat=mean ;
rowaxis values=(0 to 12 by 2) ;
run;

 

palolix_0-1778000820064.png

 

However, for the variety 'GEM', I want the sequence of the variable Harvest in the graph to be 1 (Jan), 3 (Mar), 4 (Apr), 5 (May) ,6(Jun), 8(Aug), but even if I do not include the latest format specification I was using for the variety 'I49L' in the code for 'GEM', it will still use it for 'GEM'. Maybe I am not ending the format used for 'I49L' properly.

 

proc sgpanel data=Gem;
where Harvest in(1,3,4,5,6,8) and Variety in('GEM','Hass');
panelby Harvest/ onepanel spacing=5 novarname;
styleattrs DATACONTRASTCOLORS=(BLUE red);
vline Wks/lineattrs=(pattern=solid thickness=2) response=DTR group=Variety grouporder=data markers stat=mean ;
rowaxis values=(0 to 20 by 2) ;
run;

 

palolix_1-1778001050903.png

 

 

Tom
Super User Tom
Super User

I still do not understand what your data is. Please share some data. And explain it. 

So you have variable named HARVEST that appears to mean either a MONTH number or an ORDER number (or perhaps both?).  Perhaps you should split that information into two separate variables?

 

You have one data step that maps HARVEST into a new variable named TAG.  But it only does that for some of the observations.  For the other observations you do not give TAG a value. Was TAG an existing variable already?  How is it related to HARVEST.

 

And then later you replace the HARVEST/TAG variable in the graph with a third variable named WKS.   What is the meaning of WKS?  What types of values does it have?  From your last picture it seems to have also had the HARVEST. format attached to it and so it looks like it has values of 1,3,4,5,6 and 8.  What do those values mean?

 

palolix
Lapis Lazuli | Level 10

The variable Harvest is the month the fruit was harvested. What I want to change is the order those months appear in the graph for some fruit varieties like 'I49L'.

 

I use tag only as a way to change the order of the months  in the case of 'I49L'. So the specification if (Harvest eq 11) then tag=1 will mean that the month 11 (Nov) will appear as the first month in the graph.

 

Wks (Week) is another variable in the model, which I do not want to modify. It can be week 1, 3 or 6.

Tom
Super User Tom
Super User

Why do you change the definition of the format HARVEST in your program?

Originally it was defined with information on displaying the integers 1 to 12. Then later you replaced it with a version that only display the integers from 1 to 6.  And it displays them in different ways.  You should probably define two different formats in that case.


Remember that a FORMAT converts values into text.  So it is used when you want to display the values in a particular way.  If you want to display the values in another way then I would consider that a different format.

So perhaps something like this:

 

proc format;
value Months 
  1='Jan' 2='Feb' 3='Mar'  4='Apr'  5='May'  6='Jun' 
  7='Jul' 8='Aug' 9='Sep' 10='Oct' 11='Nov' 12='Dec'
;
value Months_I49L
  1="Nov" 2="Dec" 3="Jan" 4="Feb" 5="Mar" 6="May"
;
run;

 

 

 

 

palolix
Lapis Lazuli | Level 10

Month 1 should be Jan for all varieties. The only thing I want to change is the order these months appear in the graph for some varieties  because in the case of the variety 'I49L', the harvesting season starts in Nov, therefore I would like the graph for this variety to show Nov as the first month instead of Jan. For the variety GEM, the harvesting season starts in Jan, therefore, I would like the graph to show Jan as the first month. This is the the only thing I want to change, but maybe I should have a format for each variety like you and Ksharp are suggesting. 

 

Thank you so much Tom, and sorry for all the confusion

 

 
Tom
Super User Tom
Super User

Then why did you use WKS in your graph code as the XAXIS?

Shouldn't that be a variable that has the month (or the re-coded month)?

 

My guess is that you want to use the PANELBY feature of PROC SGPLOT to make multiply graphs on a single page where the individual graphs use different ranges of XAXIS values and different orders.

 

If that is NOT true then do what @Ksharp said and just run two PROC SGPLOT statements.  One with the set of values that use the JAN -- DEC axis and another that uses the NOV--OCT axis.

 

Or you might want to use the ability of ODS itself to organize multiple "pages" into one page.  Check out ODS LAYOUT.

 

If you want someone to help you work out the details then provide some example data, does not need to be the real data.

 

 

 

 

Tom
Super User Tom
Super User

I can make a graph that sort of looks like that using data like this:


data gem ;
  length Harvest $9 Variety $4 Wks 8 DTR 8 ;
  input Harvest -- DTR;
cards;
2020-2021 Haas 1 6.5
2020-2021 Haas 3 5.9
2020-2021 Haas 5 5.8
2020-2021 Haas 8 4.3
2020-2021 GEM  3 6.1
2020-2021 GEM  5 5.7
2020-2021 GEM  8 6.2
;

proc sgpanel data=GEM;
  panelby Harvest/ onepanel spacing=5 novarname;
  styleattrs DATACONTRASTCOLORS=(BLUE red);
  vline Wks
    /lineattrs=(pattern=solid thickness=2)
     response=DTR 
     group=Variety 
     grouporder=data 
     markers 
     stat=mean 
  ;
  rowaxis values=(0 to 20 by 2) ;
run;

Share your data (use a data step like my example above) so we can see what you are actually working with.

Screenshot 2026-05-02 at 1.37.41 PM.png

 

Ksharp
Super User
Make two version of code, one for varieties (I49L and Hass) , another for varieties (others), and make a macro to wrap them up .

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
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
  • 12 replies
  • 348 views
  • 5 likes
  • 3 in conversation