-->
Hello All,
How to display the countries alphabetically on the x-axis?
I also read the below link: Tip: Avoid alphabetical order for a categorical axis in a graph - The DO Loop
if I use CATEGORYORDER=data in my code, I will get the error.
The below code works for me, just wanted to sort the bar in alphabetical order using "proc sgplot" not "proc chart".
proc freq data=tmp;
tables country / out=freq;
run;
proc sgplot data=freq;
vbar country / response=Count datalabelattrs=(size=6) ;
xaxis label=" " fitpolicy=rotate labelattrs=(size=6) valueattrs=(size=6);
yaxis label="Number of Respondents" labelattrs=(size=6) valueattrs=(size=6);
run;
Thank you!
I don't see where you create a new variable country_name and then sorted by country_name
Works for me
data fake;
input country $ count;
cards;
1 28
173 55
109 4
127 33
2 48
20 99
188 4
;
data fake1;
set fake;
country_name=put(country,$country_.);
run;
proc sort data=fake1;
by country_name;
run;
proc sgplot data=fake1;
vbar country_name/response=count;
run;
PS: You need to show us the code you used whenever something doesn't work. Showing us the output plots are not sufficient. We need to see the code, for this problem and for all future problems, when something doesn't work.
Actually, alphabetical order is the default setting.
proc freq data=sashelp.cars;
tables make / out=freq;
run;
proc sgplot data=freq;
vbar make / response=Count datalabelattrs=(size=6) ;
xaxis label=" " fitpolicy=rotate labelattrs=(size=6) valueattrs=(size=6);
yaxis label="Number of Respondents" labelattrs=(size=6) valueattrs=(size=6);
run;
Wherase, Rick Wicklin's blog told to use frequency order and horizontal bar.
proc freq data=sashelp.cars;
tables make / out=freq;
run;
proc sgplot data=freq;
hbar make / response=Count datalabelattrs=(size=6) categoryorder=respdesc ;
xaxis label=" " fitpolicy=rotate labelattrs=(size=6) valueattrs=(size=6);
yaxis label="Number of Respondents" labelattrs=(size=6) valueattrs=(size=6);
run;
Thanks for your answer, as you see below, my issue is I can not get the bar in alphabetical order.
Show us the code you used. Is your data that you send to PROC SGPLOT sorted alphabetically? Please paste the graph with incorrect results into your message. Do not include it as a file attachment.
Thank you for your answer. My code has been mentioned in my initial comment.
The x-axis is too cluttered because there are many countries, which is why I attached the PDF in my previous comment for better clarity.
Also, even after sorting the data by country, they do not appear in alphabetical order.
proc freq data=tmp;
tables country / out=freq;run;
proc sort data=freq; by country; run;
proc sgplot data=freq;
vbar country / response=Count datalabelattrs=(size=6) ;
xaxis label=" " fitpolicy=rotate labelattrs=(size=6) valueattrs=(size=6);
yaxis label="Number of Respondents" labelattrs=(size=6) valueattrs=(size=6);
run;
What type of variable is COUNTRY? Is it NUMERIC of CHRACTER? Does it have FORMAT attached to it?
Thanks for your answer, country is the character variable, and yes there is a format in my coding like '1'='Afghanistan'
'2'='Albania'
etc
The problem comes in that if Afghanistan is '1' and you sort these then whatever country '10' is should be next. Why? Because when you sort alphabetically, after '1' the next sorted value will be '10'.
If you want to sort by country name alphabetically you probably need to create a variable that contains the country name and sort that.
length country_name $ 40;
country_name=put(country,$formatname.);
or by changing the sort (and assuming your numbers in the format are always in alphabetical order of the country_names)
proc sort data=freq sortseq=linguistic(numeric_collation=ON);
by country;
run;
I tried this way, with your first way, the plot is like below: it ignores the name of the country
with your second way, would be like below: didn't sort
Show us the PROC FORMAT you are using.
proc format ;
value $country_ '1'='Afghanistan' '2'='Albania'
'3'='Algeria' '4'='Andorra'
'5'='Angola' '6'='Antigua and Barbuda'
'7'='Argentina' '8'='Armenia'
'9'='Australia' '10'='Austria'
'11'='Azerbaijan' '12'='Bahamas'
'13'='Bahrain' '14'='Bangladesh'
'15'='Barbados' '16'='Belarus'
'17'='Belgium' '18'='Belize'
'19'='Benin' '20'='Bhutan'
'21'='Bolivia' '22'='Bosnia and Herzegovina'
'23'='Botswana' '24'='Brazil'
'25'='Brunei' '26'='Bulgaria'
'27'='Burkina Faso' '28'='Burundi'
'29'='Cabo Verde' '30'='Cambodia'
'31'='Cameroon' '32'='Canada'
'33'='Central African Republic' '34'='Chad'
'35'='Chile' '36'='China'
'37'='Colombia' '38'='Comoros'
'39'='Costa Rica'
'40'='Côte Ivoire' '41'='Croatia'
'42'='Cuba' '43'='Cyprus'
'44'='Czech Republic' '45'='Denmark'
'46'='Djibouti' '47'='Dominica'
'48'='Dominican Republic' '49'='East Timor (Timor-Leste)'
'50'='Ecuador' '51'='Egypt'
'52'='El Salvador' '53'='Equatorial Guinea'
'54'='Eritrea' '55'='Estonia'
'56'='Eswatini' '57'='Ethiopia'
'58'='Fiji' '59'='Finland'
'60'='France' '61'='Gabon'
'62'='The Gambia' '63'='Georgia'
'64'='Germany' '65'='Ghana'
'66'='Greece' '67'='Grenada'
'68'='Guatemala' '69'='Guinea'
'70'='Guinea-Bissau' '71'='Guyana'
'72'='Haiti' '73'='Honduras'
'74'='Hungary' '75'='Iceland'
'76'='India' '77'='Indonesia'
'78'='Iran' '79'='Iraq'
'80'='Ireland' '81'='Israel'
'82'='Italy' '83'='Jamaica'
'84'='Japan' '85'='Jordan'
'86'='Kazakhstan' '87'='Kenya'
'88'='Kiribati' 'Korea'='South'
'89'='Kosovo' '90'='Kuwait'
'91'='Kyrgyzstan' '92'='Laos'
'93'='Latvia' '94'='Lebanon'
'95'='Lesotho' '96'='Liberia'
'97'='Libya' '98'='Liechtenstein'
'99'='Lithuania' '100'='Luxembourg'
'101'='Madagascar' '102'='Malawi'
'103'='Malaysia' '104'='Maldives'
'105'='Mali' '106'='Malta'
'107'='Marshall Islands' '108'='Mauritania'
'109'='Mauritius' '110'='Mexico'
'211'='Micronesia' '111'='Moldova'
'112'='Monaco' '113'='Mongolia'
'114'='Montenegro' '115'='Morocco'
'116'='Mozambique' '117'='Myanmar'
'118'='Namibia' '119'='Nauru'
'120'='Nepal' '121'='Netherlands'
'122'='New Zealand' '123'='Nicaragua'
'124'='Niger' '125'='Nigeria'
'126'='North Macedonia' '127'='Norway'
'128'='Oman' '129'='Pakistan'
'130'='Palau' '131'='Panama'
'132'='Papua New Guinea' '133'='Paraguay'
'134'='Peru' '135'='Philippines'
'136'='Poland' '137'='Portugal'
'138'='Qatar' '139'='Romania'
'140'='Russia' '141'='Rwanda'
'142'='Saint Kitts and Nevis' '143'='Saint Lucia'
'144'='Saint Vincent and the Grenadines' '145'='Samoa'
'146'='San Marino' '147'='Sao Tome and Principe'
'148'='Saudi Arabia' '149'='Senegal'
'150'='Serbia' '151'='Seychelles'
'152'='Sierra Leone' '153'='Singapore'
'154'='Slovakia' '155'='Slovenia'
'156'='Solomon Islands' '157'='Somalia'
'158'='South Africa' '159'='Spain'
'160'='Sri Lanka' '161'='Sudan'
'Sudan'='South' '162'='Suriname'
'163'='Sweden' '164'='Switzerland'
'165'='Syria' '166'='Taiwan'
'167'='Tajikistan' '168'='Tanzania'
'169'='Thailand' '170'='Togo'
'171'='Tonga' '172'='Trinidad and Tobago'
'173'='Tunisia' '174'='Turkey'
'175'='Turkmenistan' '176'='Tuvalu'
'177'='Uganda' '178'='Ukraine'
'179'='United Arab Emirates' '180'='United Kingdom'
'181'='United States' '182'='Uruguay'
'183'='Uzbekistan' '184'='Vanuatu'
'185'='Vatican City' '186'='Venezuela'
'187'='Vietnam' '188'='Yemen'
'189'='Zambia' '190'='Zimbabwe'
run;
I don't see where you create a new variable country_name and then sorted by country_name
Works for me
data fake;
input country $ count;
cards;
1 28
173 55
109 4
127 33
2 48
20 99
188 4
;
data fake1;
set fake;
country_name=put(country,$country_.);
run;
proc sort data=fake1;
by country_name;
run;
proc sgplot data=fake1;
vbar country_name/response=count;
run;
PS: You need to show us the code you used whenever something doesn't work. Showing us the output plots are not sufficient. We need to see the code, for this problem and for all future problems, when something doesn't work.
Please remember to show us the code from now on.
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.
Ready to level-up your skills? Choose your own adventure.