
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: CRSPIN1 */
/* TITLE: Correspondence Analysis of Number Of Ph.D.'S Awarded*/
/* PRODUCT: STAT */
/* SYSTEM: ALL */
/* KEYS: marketing research, categorical data analysis */
/* PROCS: CORRESP PLOT */
/* DATA: */
/* */
/* REF: PROC CORRESP, INTRODUCTORY EXAMPLE 1. */
/* MISC: THIS EXAMPLE HAS BEEN MODIFIED SINCE IT APPEARED */
/* IN THE RELEASE 6.06 SAS/STAT USER'S GUIDE */
/* */
/****************************************************************/
data phds;
input science $ 1-19 y1973-y1978;
label y1973 = '1973'
y1974 = '1974'
y1975 = '1975'
y1976 = '1976'
y1977 = '1977'
y1978 = '1978'
;
cards;
Life Sciences 4489 4303 4402 4350 4266 4361
Physical Sciences 4101 3800 3749 3572 3410 3234
Social Sciences 3354 3286 3344 3278 3137 3008
Behavioral Sciences 2444 2587 2749 2878 2960 3049
Engineering 3338 3144 2959 2791 2641 2432
Mathematics 1222 1196 1149 1003 959 959
;
proc corresp data=phds out=results observed rp short;
var y1973-y1978;
id science;
run;
proc plot vtoh=2;
plot dim1 * dim2 = '*' $ science / box haxis=by 0.15 vaxis=by 0.15;
run;
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: CRSPTAB1 */
/* TITLE: PROC CORRESP Tables Statement Illustrations */
/* PRODUCT: STAT */
/* SYSTEM: ALL */
/* KEYS: marketing research, categorical data analysis */
/* PROCS: CORRESP PLOT */
/* DATA: */
/* */
/* REF: PROC CORRESP, TABLES EXAMPLE 1. */
/* MISC: THIS EXAMPLE HAS BEEN MODIFIED SINCE IT APPEARED */
/* IN THE RELEASE 6.06 SAS/STAT USER'S GUIDE */
/* */
/****************************************************************/
data neighbor;
input surname $ 1-10 age $ 12-18 sex $ 19-25
height $ 26-30 hair $ 32-37;
cards;
jones old male short white
smith young female tall brown
kasavitz old male short brown
ernst old female tall white
zannoria old female short brown
spangel young male tall blond
myers young male tall brown
kasinski old male short blond
colman young female short blond
delafave old male tall brown
singer young male tall brown
igor old short
;
proc corresp data=neighbor dimens=1 observed short;
tables sex,age;
run;
proc corresp data=neighbor cross=row observed short;
tables hair height , sex age;
run;
proc corresp data=neighbor observed short mca;
tables hair height sex age;
supplementary age;
run;
proc corresp data=neighbor observed short mca;
tables hair height sex age;
run;
*---Use a data step, PROC CORRESP to analyze an indicator matrix---;
*---Igor's observation with missing values is excluded. ---;
data n2;
set neighbor;
n= _n_; /* observation number */
run;
proc corresp data=n2 short observed;
tables n, age sex height hair;
run;
data neighbor;
input surname $ 1-10 age $ 12-18 sex $ 19-25
height $ 26-30 hair $ 32-37;
array v {9} old young female male short tall blond brown white;
do i = 1 to 9;
v{i} = 0;
end;
if age = 'old ' then old = 1;
else if age = 'young' then young = 1;
else if age = ' ' then do;
old = 0.5;
young = 0.5;
end;
if sex = 'female' then female = 1;
else if sex = 'male ' then male = 1;
else if sex = ' ' then do;
female = 0.5;
male = 0.5;
end;
if height = 'short' then short = 1;
else if height = 'tall ' then tall = 1;
else if height = ' ' then do;
short = 0.5;
tall = 0.5;
end;
if hair = 'white' then white = 1;
else if hair = 'brown' then brown = 1;
else if hair = 'blond' then blond = 1;
else if hair = ' ' then do;
white = 1/3;
brown = 1/3;
blond = 1/3;
end;
drop i;
cards;
jones old male short white
smith young female tall brown
kasavitz old male short brown
ernst old female tall white
zannoria old female short brown
spangel young male tall blond
myers young male tall brown
kasinski old male short blond
colman young female short blond
delafave old male tall brown
singer young male tall brown
igor old short
;
proc print;
run;
data a;
input row $ col $ freq;
cards;
A Q 1
A P 3
B Q 2
B P 1
A Q 4
A P 5
;
proc corresp freqout observed dimens=1;
tables row, col;
weight freq;
run;
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: CRSPEX1 */
/* TITLE: Correspondence Analysis of Car Owners And Car Origin*/
/* PRODUCT: STAT */
/* SYSTEM: ALL */
/* KEYS: marketing research, categorical data analysis */
/* PROCS: CORRESP PLOT */
/* DATA: */
/* */
/* REF: PROC CORRESP, EXAMPLE 1. */
/* MISC: THIS EXAMPLE HAS BEEN MODIFIED SINCE IT APPEARED */
/* IN THE RELEASE 6.06 SAS/STAT USER'S GUIDE */
/* */
/****************************************************************/
title 'Car Owners and Car Origin';
data cars;
missing a;
/* Read Numeric Variables */
input (norigin nsize ntype nhome nincome nmarital nkids nsex)
(1.) @@;
/* Check for End of Line */
if n(of norigin -- nsex) eq 0 then do;
input;
return;
end;
/* Create Character Variables */
if norigin = 1 then origin = 'American';
else if norigin = 2 then origin = 'Japanese';
else if norigin = 3 then origin = 'European';
if nsize = 1 then size = 'Small ';
else if nsize = 2 then size = 'Medium ';
else if nsize = 3 then size = 'Large ';
if ntype = 1 then type = 'Family ';
else if ntype = 2 then type = 'Sporty ';
else if ntype = 3 then type = 'Work ';
if nhome = 1 then home = 'Own ';
else home = 'Rent ';
if nsex = 1 then sex = 'Male ';
else sex = 'Female ';
if nincome = 1 then income = '1 Income ';
else income = '2 Incomes';
if nkids > 0 then do;
if nmarital = 1 then marital = 'Single w Kids ';
else marital = 'Married w Kids';
end;
else do;
if nmarital = 1 then marital = 'Single ';
else marital = 'Married ';
end;
keep marital origin;
output;
return;
cards;
131112212121110121112201131211011211221122112121131122123211222212212201
121122023121221232211101122122022121110122112102131112211121110112311101
211112113211223121122202221122111311123131211102321122223221220221221101
122122022121220211212201221122021122110132112202213112111331226122221101
1212110231AA220232112212113112112121220212212202112111022222110212121221
211211012211222212211101313112113121220121112212121112212211222221112211
221111011112220122212201131211013121220113112222131112012131110221112211
121112212211121121112201321122311311221113112212213211013121220221221101
133211011212220233311102213111023211122121312222212212111111222121112211
133112011212112212112212212222022131222222121101111122022211220113112212
211112012232220121221102213211011131220121212201211122112331220233312202
222122012111220212112201221122112212220222212211311122012111110112212212
112222011131112221212202322211021222110121221101333211012232110132212101
223222013111220112211101211211022112110212211102221122021111220112111211
111122022121110113311122322111122221210222211101212122021211221232112202
1331110113112211213222012131221211112212221122021331220212121112121.2212
121122.22121210233112212222121011311122121211102211122112121110121212101
311212022231221112112211211211312221221213112212221122022222110131212202
213122211311221212112222113122221221220213111221121211221211221221221102
131122211211220221222101223112012111221212111102223122111311222121111102
2121110121112202133122222311122121312212112.2101312122012111122112112202
111212023121110111112221212111012211220221321101221211122121220112111112
212211022111110122221101121112112122110122122232221122212211221212112202
213122112211110212121201113211012221110232111102212211012112220121212202
221112011211220121221101211211022211221112121101111112212121221111221201
211122122122111212112221111122312132110113121101121122222111220222121102
221211012122110221221102312111012122220121121101121122221111222212221102
212122021222120113112202121122212121110113111101123112212111220113111101
221112211321210131212211121211011222110122112222123122023121223112212202
311211012131110131221102112211021131220213122201222111022121221221312202
131.22523221110122212221131112412211220221121112131222022122220122122201
212111011311220221312202221122123221210121222202223122121211221221111112
211111121211221221212201113122122131220222112222211122011311110112312211
211222013221220121211211312122122221220122112201111222011211110122311112
312111021231220122121101211112112.22110222112212121122122211110121112101
121211013211222121112222321112112112110121321101113111012221220121312201
213211012212220221211101321122121111220221121101122211021122110213112212
212122011211122131221101121211022212220212121101
;
*---Perform Simple Correspondence Analysis---;
proc corresp all data=cars outc=coor;
tables marital, origin;
run;
*---Plot the Simple Correspondence Analysis Results---;
proc plot vtoh=2;
plot dim2 * dim1 ='*' $ _name_ / box haxis=by 0.1 vaxis=by 0.1;
run; quit;
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: CRSPEX2 */
/* TITLE: MCA of Car Owners And Car Origin */
/* PRODUCT: STAT */
/* SYSTEM: ALL */
/* KEYS: marketing research, categorical data analysis */
/* PROCS: CORRESP PLOT */
/* DATA: */
/* */
/* REF: PROC CORRESP, EXAMPLE 2. */
/* MISC: THIS EXAMPLE HAS BEEN MODIFIED SINCE IT APPEARED */
/* IN THE RELEASE 6.06 SAS/STAT USER'S GUIDE */
/* */
/****************************************************************/
title 'Car Owners and Car Origin';
data cars;
missing a;
/* Read Numeric Variables */
input (norigin nsize ntype nhome nincome nmarital nkids nsex)
(1.) @@;
/* Check for End of Line */
if n(of norigin -- nsex) eq 0 then do;
input;
return;
end;
/* Create Character Variables */
if norigin = 1 then origin = 'American';
else if norigin = 2 then origin = 'Japanese';
else if norigin = 3 then origin = 'European';
if nsize = 1 then size = 'Small ';
else if nsize = 2 then size = 'Medium ';
else if nsize = 3 then size = 'Large ';
if ntype = 1 then type = 'Family ';
else if ntype = 2 then type = 'Sporty ';
else if ntype = 3 then type = 'Work ';
if nhome = 1 then home = 'Own ';
else home = 'Rent ';
if nsex = 1 then sex = 'Male ';
else sex = 'Female ';
if nincome = 1 then income = '1 Income ';
else income = '2 Incomes';
if nkids > 0 then do;
if nmarital = 1 then marital = 'Single w Kids ';
else marital = 'Married w Kids';
end;
else do;
if nmarital = 1 then marital = 'Single ';
else marital = 'Married ';
end;
output;
return;
cards;
131112212121110121112201131211011211221122112121131122123211222212212201
121122023121221232211101122122022121110122112102131112211121110112311101
211112113211223121122202221122111311123131211102321122223221220221221101
122122022121220211212201221122021122110132112202213112111331226122221101
1212110231AA220232112212113112112121220212212202112111022222110212121221
211211012211222212211101313112113121220121112212121112212211222221112211
221111011112220122212201131211013121220113112222131112012131110221112211
121112212211121121112201321122311311221113112212213211013121220221221101
133211011212220233311102213111023211122121312222212212111111222121112211
133112011212112212112212212222022131222222121101111122022211220113112212
211112012232220121221102213211011131220121212201211122112331220233312202
222122012111220212112201221122112212220222212211311122012111110112212212
112222011131112221212202322211021222110121221101333211012232110132212101
223222013111220112211101211211022112110212211102221122021111220112111211
111122022121110113311122322111122221210222211101212122021211221232112202
1331110113112211213222012131221211112212221122021331220212121112121.2212
121122.22121210233112212222121011311122121211102211122112121110121212101
311212022231221112112211211211312221221213112212221122022222110131212202
213122211311221212112222113122221221220213111221121211221211221221221102
131122211211220221222101223112012111221212111102223122111311222121111102
2121110121112202133122222311122121312212112.2101312122012111122112112202
111212023121110111112221212111012211220221321101221211122121220112111112
212211022111110122221101121112112122110122122232221122212211221212112202
213122112211110212121201113211012221110232111102212211012112220121212202
221112011211220121221101211211022211221112121101111112212121221111221201
211122122122111212112221111122312132110113121101121122222111220222121102
221211012122110221221102312111012122220121121101121122221111222212221102
212122021222120113112202121122212121110113111101123112212111220113111101
221112211321210131212211121211011222110122112222123122023121223112212202
311211012131110131221102112211021131220213122201222111022121221221312202
131.22523221110122212221131112412211220221121112131222022122220122122201
212111011311220221312202221122123221210121222202223122121211221221111112
211111121211221221212201113122122131220222112222211122011311110112312211
211222013221220121211211312122122221220122112201111222011211110122311112
312111021231220122121101211112112.22110222112212121122122211110121112101
121211013211222121112222321112112112110121321101113111012221220121312201
213211012212220221211101321122121111220221121101122211021122110213112212
212122011211122131221101121211022212220212121101
;
*---Perform Multiple Correspondence Analysis---;
proc corresp mca observed data=cars outc=coor;
tables origin size type income home marital sex;
run;
*---Plot the Multiple Correspondence Analysis Results---;
proc plot vtoh=2;
plot dim2 * dim1 ='*' $ _name_ / box haxis=by 0.5 vaxis=by 0.5;
run; quit;
/****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: CRSPEX3 */
/* TITLE: Correspondence Analysis of United States Population */
/* PRODUCT: STAT */
/* SYSTEM: ALL */
/* KEYS: marketing research, categorical data analysis */
/* PROCS: CORRESP PLOT */
/* DATA: */
/* */
/* REF: PROC CORRESP, EXAMPLE 3. */
/* MISC: THIS EXAMPLE HAS BEEN MODIFIED SINCE IT APPEARED */
/* IN THE RELEASE 6.06 SAS/STAT USER'S GUIDE */
/* */
/****************************************************************/
title 'United States Population';
data uspop;
*----------------------------------------------------------------+
| |
| Regions: |
| New England - Maine, NH, Vt, Mass, RI, Conn. |
| NY NJ Pa - NY, NJ, Pa. |
| Great Lake - Ohio, Ind, Ill, Mich, Wis. |
| Midwest - Minn, Iowa, Mo, N Dak, S Dak, Neb, Kan. |
| South Atlantic - Del, Md, DC, Va, W Va, NC, SC, Ga, Fla. |
| Ky Ten Ala Mis - Ky, Tenn, Ala, Miss. |
| Ar La Ok Tex - Ark, La, Okla, Tex. |
| Mountain - Mon, Ida, Wyo, Col, N Mex, Ari, Utah, Nev. |
| Pacific - Wash, Oreg, Cal. |
| |
| Note: Multiply data values by 1000 to get populations. |
| |
+----------------------------------------------------------------;
/* Read Contingency Table */
input region $ 15. y1920 y1930 y1940 y1950 y1960 y1970;
label y1920 = '1920'
y1930 = '1930'
y1940 = '1940'
y1950 = '1950'
y1960 = '1960'
y1970 = '1970'
;
/* Flag Supplementary Observations */
if region = 'Hawaii' or region = 'Alaska'
then w = -1000;
else w = 1000;
cards;
New England 7401 8166 8437 9314 10509 11842
NY NJ Pa 22261 26261 27539 30146 34168 37199
Great Lake 21476 25297 26626 30399 36225 40252
Midwest 12544 13297 13517 14061 15394 16319
South Atlantic 13990 15794 17823 21182 25972 30671
Ky Ten Ala Mis 8893 9887 10778 11447 12050 12803
Ar La Ok Tex 10242 12177 13065 14538 16951 19321
Mountain 3336 3702 4150 5075 6855 8282
Pacific 5567 8195 9733 14486 20339 25454
Alaska 55 59 73 129 226 300
Hawaii 256 368 423 500 633 769
;
*---Perform Simple Correspondence Analysis---;
proc corresp print=percent observed cellchi2 rp cp
short outc=coor;
var y1920 -- y1970;
id region;
weight w;
run;
*---Plot the Simple Correspondence Analysis Results---;
proc plot vtoh=2;
plot dim1 * dim2 = '*' $ region /
box haxis=-0.2 to 0.2 by 0.1 vaxis=by 0.1;
label dim1 = 'Dimension 1'
dim2 = 'Dimension 2';
run; quit;
*----------------------------------------------------------------+
| |
| The code below this point is from the 6.06 example. The calls |
| to macro equate have been disabled. |
| |
+----------------------------------------------------------------;
*---Create Annotate Data Set---;
data coor;
set coor;
y = dim1; /* y-axis variable */
x = dim2; /* x-axis variable */
xsys = '2'; /* x-axis label area is axis min to axis max */
ysys = '2'; /* y-axis label area is axis min to axis max */
text = region; /* text string to position in the plot */
size = 1.2; /* relative size of the text string */
label y = 'Dimension 1'
x = 'Dimension 2';
keep x y text xsys ysys size;
run;
%macro equate(xmax=6.5, /* maximum x axis inches */
ymax=8.5, /* maximum y axis inches */
xinc=0.1, /* x axis tick increment */
yinc=0.1, /* y axis tick increment */
xpextra=0, /* include extra + end x axis ticks */
xmextra=0, /* include extra - end x axis ticks */
ypextra=0, /* include extra + end y axis ticks */
ymextra=0, /* include extra - end y axis ticks */
data=coor); /* input data set */
/* Macro EQUATE creates a GPLOT of point labels with */
/* equated axes. */
/* */
/* This macro requires SAS/GRAPH software and a GOPTIONS */
/* statement. */
/* */
/* For vertical axis variable Y it creates an AXIS1 */
/* statement and for horizontal */
/* axis variable X it creates */
/* an AXIS2 statement such that an inch on the vertical */
/* axis represents the same data range as an inch on the */
/* horizontal axis. The 'extra' parameters are used to */
/* include extra ticks beyond the range of the data to */
/* accommodate long point labels within the boundaries */
/* of the plot. */
/* */
/* Reset the defaults to be more suited to your devices. */
/* With minor modifications, other options could be */
/* specified. */
/* */
/* This macro performs no error checking. */
/* Find the Minima and Maxima */
proc means noprint data=&data;
var y x;
output out=__temp__ min=ymin xmin max=ymax xmax;
options nonotes;
run;
data _null_;
set __temp__;
/* Scale Minima and Maxima to Multiples of the Increments */
yinc = &yinc;
xinc = &xinc;
ymin = (floor(ymin / yinc) - (&ymextra)) * yinc;
xmin = (floor(xmin / xinc) - (&xmextra)) * xinc;
ymax = (ceil (ymax / yinc) + (&ypextra)) * yinc;
xmax = (ceil (xmax / xinc) + (&xpextra)) * xinc;
/* Compute the Axis Lengths */
ytox = (ymax - ymin) / (xmax - xmin);
if ytox le ((&ymax) / (&xmax)) then do;
xlen = &xmax;
ylen = (&xmax) * ytox;
end;
else do;
ylen = &ymax;
xlen = (&ymax) / ytox;
end;
/* Write Results to Symbolic Variables */
call symput('len1',ylen);
call symput('len2',xlen);
call symput('min1',ymin);
call symput('min2',xmin);
call symput('max1',ymax);
call symput('max2',xmax);
call symput('inc1',yinc);
call symput('inc2',xinc);
run;
options notes;
/* Write the Generated Statements to the Log */
%put NOTE: The following statements were generated.;
%put proc gplot data=&data%str(;);
%put axis1 length=&len1 IN order=&min1 to &max1 by &inc1%str(;);
%put axis2 length=&len2 IN order=&min2 to &max2 by &inc2%str(;);
%put symbol1 v=none%str(;);
%put plot y*x=1 / annotate=&data frame haxis=axis2 vaxis=axis1;
%put href=0 vref=0%str(;);
%put run%str(;);
/* Create the GPLOT */
proc gplot data=&data;
axis1 length=&len1 IN order=&min1 to &max1 by &inc1;
/* Note: Lengths are Device Specific. */
axis2 length=&len2 IN order=&min2 to &max2 by &inc2;
symbol1 v=none;
plot y*x=1 / annotate=&data frame haxis=axis2 vaxis=axis1
href=0 vref=0;
run;
%mend equate;
/* Note, disabled due to PROC PLOT label plot availability
%equate()
%equate(xpextra=1,xmextra=1) */
|