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

/* Dear all, I would like to know how I could:

  1. Create space for the different levels of the region variable so that the horizontal bars between, for example, Africa and Asia are visually separated more clearly.
  2. Organize the graph so that the first horizontal bar is for the region with the highest growth_rate in 2024, the second region (the one below the region with the highest growth_rate for 2024) is the one with the second highest growth_rate in 2024, and so on, so that the last region is the one with the lowest growth_rate for 2024.
  3. Bold and color in red only "North America" and "South America" on the Y-axis (i.e., I want to draw more attention to these two regions, which are the two I focus my analysis on).

Below is the dataset with the graph I managed to build so far. Unfortunately, I couldn't find a way to achieve the three steps above. Does anyone have any idea how to do it?

Any help is very welcome. Thank you!

*/
data have;
input code region $13. growth_rate year;
datalines;
0 North America -5.92 2020
0 North America -2.18 2021
0 North America -7.47 2022
0 North America -2.17 2023
0 North America -9.64 2024
1 South America 0.36 2020
1 South America 2.69 2021
1 South America -8.13 2022
1 South America 1.22 2023
1 South America -1.34 2024
2 Asia -1.53 2020
2 Asia 2.18 2021
2 Asia 5.64 2022
2 Asia 6.80 2023
2 Asia 8.70 2024
3 Europe -9.68 2020
3 Europe 3.91 2021
3 Europe 5.48 2022
3 Europe 3.52 2023
3 Europe 3.69 2024
4 Africa 3.29 2020
4 Africa 7.97 2021
4 Africa 2.48 2022
4 Africa 7.36 2023
4 Africa 1.31 2024
5 Oceania 8.00 2020
5 Oceania -6.05 2021
5 Oceania 6.91 2022
5 Oceania -1.09 2023
5 Oceania -7.46 2024
;
run;


proc sgplot data=have;
title "Growth rate";
styleattrs datacolors=(white blue red orange black)
datacontrastcolors=(white black black black black)
datafillpatterns=(r1 r1 l1 x1 l5);

hbar region / response=growth_rate group=year
groupdisplay=cluster
clusterwidth=0.9
datalabel
datalabelattrs=(size=8pt)
barwidth=1 nooutline
transparency=0 dataskin=crisp fillpattern;

xaxis display=(nolabel) min=-12 max=12;
yaxis display=(nolabel);
run;



1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

You want this ?

 

data have;
input code region &$20. growth_rate year;
datalines;
0 North America  -5.92 2020
0 North America  -2.18 2021
0 North America  -7.47 2022
0 North America  -2.17 2023
0 North America  -9.64 2024
1 South America  0.36 2020
1 South America  2.69 2021
1 South America  -8.13 2022
1 South America  1.22 2023
1 South America  -1.34 2024
2 Asia  -1.53 2020
2 Asia  2.18 2021
2 Asia  5.64 2022
2 Asia  6.80 2023
2 Asia  8.70 2024
3 Europe  -9.68 2020
3 Europe  3.91 2021
3 Europe  5.48 2022
3 Europe  3.52 2023
3 Europe  3.69 2024
4 Africa  3.29 2020
4 Africa  7.97 2021
4 Africa  2.48 2022
4 Africa  7.36 2023
4 Africa  1.31 2024
5 Oceania  8.00 2020
5 Oceania  -6.05 2021
5 Oceania  6.91 2022
5 Oceania  -1.09 2023
5 Oceania  -7.46 2024
;
run;

proc sort data=have(where=(year=2024)) out=temp;
by growth_rate  ;
run;
data fmt;
 set temp(rename=(region=start));
 retain fmtname 'fmt' type 'c';
 length label $ 80;
 label=repeat(' ',_n_)||strip(start);
 keep fmtname type start label;
run;
proc format cntlin=fmt;
run;
data have2;
 set have;
 _region=put(region,$fmt32.);
 if region="North America" then call symput('NorthAmerica',_region);
 if region="South America" then call symput('SouthAmerica',_region);
run;


%sganno
data sganno;
 %SGTEXT(LABEL="North America",TEXTWEIGHT="BOLD",TEXTCOLOR="RED",TEXTSIZE=10, FILLCOLOR="white",FILLTRANSPARENCY=0,WIDTH=40,Y1SPACE="DATAVALUE",X1SPACE="LAYOUTPERCENT",YC1="&NorthAmerica.",X1=6 )
 %SGTEXT(LABEL="South America",TEXTWEIGHT="BOLD",TEXTCOLOR="RED",TEXTSIZE=10, FILLCOLOR="white",FILLTRANSPARENCY=0,WIDTH=40,Y1SPACE="DATAVALUE",X1SPACE="LAYOUTPERCENT",YC1="&SouthAmerica" ,X1=6 )
run;


proc sgplot data=have2 sganno=sganno;
title "Growth rate";
styleattrs datacolors=(white blue red orange black)
datacontrastcolors=(white black black black black)
datafillpatterns=(r1 r1 l1 x1 l5);

hbar _region / response=growth_rate group=year 
groupdisplay=cluster
clusterwidth=0.9
datalabel DATALABELFITPOLICY=NONE
datalabelattrs=(size=8pt)
barwidth=1 nooutline
transparency=0 dataskin=crisp fillpattern;

xaxis display=(nolabel) min=-12 max=12;
yaxis display=(nolabel) colorbands=even ;
run;

Ksharp_0-1741231645738.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Please re-post the code for creating your example data in a text box. Open the text box by clicking on the </> icon and paste the code. The main message window reformats pasted code and so the there is no space between the end of the Region values and so we get "Region" values that look like "Asia -1.53 20" because of reading 13 characters. Also several of the lines end up to short and get the log note

 

NOTE: SAS went to a new line when INPUT statement reached past the end of
      a line.

Which usually indicates a read problem.

 

 

 

 

 

MFraga
Quartz | Level 8

Sorry, I did not know that the & modifier in the input statement indicates that the character variable can contain spaces. This allows SAS to read the variable until it encounters two or more consecutive spaces or the end of the line. Thanks @Ksharp for correcting my code.

 

Here it is posted again:



data have;
input code region &$20. growth_rate year;
datalines;
0 North America  -5.92 2020
0 North America  -2.18 2021
0 North America  -7.47 2022
0 North America  -2.17 2023
0 North America  -9.64 2024
1 South America  0.36 2020
1 South America  2.69 2021
1 South America  -8.13 2022
1 South America  1.22 2023
1 South America  -1.34 2024
2 Asia  -1.53 2020
2 Asia  2.18 2021
2 Asia  5.64 2022
2 Asia  6.80 2023
2 Asia  8.70 2024
3 Europe  -9.68 2020
3 Europe  3.91 2021
3 Europe  5.48 2022
3 Europe  3.52 2023
3 Europe  3.69 2024
4 Africa  3.29 2020
4 Africa  7.97 2021
4 Africa  2.48 2022
4 Africa  7.36 2023
4 Africa  1.31 2024
5 Oceania  8.00 2020
5 Oceania  -6.05 2021
5 Oceania  6.91 2022
5 Oceania  -1.09 2023
5 Oceania  -7.46 2024
;
run;

I think it works now.

Ksharp
Super User

You want this ?

 

data have;
input code region &$20. growth_rate year;
datalines;
0 North America  -5.92 2020
0 North America  -2.18 2021
0 North America  -7.47 2022
0 North America  -2.17 2023
0 North America  -9.64 2024
1 South America  0.36 2020
1 South America  2.69 2021
1 South America  -8.13 2022
1 South America  1.22 2023
1 South America  -1.34 2024
2 Asia  -1.53 2020
2 Asia  2.18 2021
2 Asia  5.64 2022
2 Asia  6.80 2023
2 Asia  8.70 2024
3 Europe  -9.68 2020
3 Europe  3.91 2021
3 Europe  5.48 2022
3 Europe  3.52 2023
3 Europe  3.69 2024
4 Africa  3.29 2020
4 Africa  7.97 2021
4 Africa  2.48 2022
4 Africa  7.36 2023
4 Africa  1.31 2024
5 Oceania  8.00 2020
5 Oceania  -6.05 2021
5 Oceania  6.91 2022
5 Oceania  -1.09 2023
5 Oceania  -7.46 2024
;
run;

proc sort data=have(where=(year=2024)) out=temp;
by growth_rate  ;
run;
data fmt;
 set temp(rename=(region=start));
 retain fmtname 'fmt' type 'c';
 length label $ 80;
 label=repeat(' ',_n_)||strip(start);
 keep fmtname type start label;
run;
proc format cntlin=fmt;
run;
data have2;
 set have;
 _region=put(region,$fmt32.);
 if region="North America" then call symput('NorthAmerica',_region);
 if region="South America" then call symput('SouthAmerica',_region);
run;


%sganno
data sganno;
 %SGTEXT(LABEL="North America",TEXTWEIGHT="BOLD",TEXTCOLOR="RED",TEXTSIZE=10, FILLCOLOR="white",FILLTRANSPARENCY=0,WIDTH=40,Y1SPACE="DATAVALUE",X1SPACE="LAYOUTPERCENT",YC1="&NorthAmerica.",X1=6 )
 %SGTEXT(LABEL="South America",TEXTWEIGHT="BOLD",TEXTCOLOR="RED",TEXTSIZE=10, FILLCOLOR="white",FILLTRANSPARENCY=0,WIDTH=40,Y1SPACE="DATAVALUE",X1SPACE="LAYOUTPERCENT",YC1="&SouthAmerica" ,X1=6 )
run;


proc sgplot data=have2 sganno=sganno;
title "Growth rate";
styleattrs datacolors=(white blue red orange black)
datacontrastcolors=(white black black black black)
datafillpatterns=(r1 r1 l1 x1 l5);

hbar _region / response=growth_rate group=year 
groupdisplay=cluster
clusterwidth=0.9
datalabel DATALABELFITPOLICY=NONE
datalabelattrs=(size=8pt)
barwidth=1 nooutline
transparency=0 dataskin=crisp fillpattern;

xaxis display=(nolabel) min=-12 max=12;
yaxis display=(nolabel) colorbands=even ;
run;

Ksharp_0-1741231645738.png

 

MFraga
Quartz | Level 8

Wow! That's really a great solution! From now on, I will be able to finish customizing the graph on my own. I confess that I didn't know about the sganno option in sgplot. It is indeed interesting, I will continue to explore it further. Thanks a lot!

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

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
  • 4 replies
  • 1362 views
  • 2 likes
  • 3 in conversation