FORS8000
Week 9: Light at the end of the
tunnel?-- getting your data in order for analyses
Save example SAS dataset speciesx to a disk or on your hard drive. Create a library named "easy" and place the SAS file there.
/***create a working (copy) datafile in the work directory and print**/
data species2; set easy.species;
proc print data = species2;
run;
/** create a datafile dog using species2 containing rows (records) where species equals dog and print ***/
data dog;
set species2;
where species = 'dog';
proc print;
run;
/*** Create datafile T_dog by transposing the variable (column) 'count' in the dog datafile **/
proc transpose data = dog out = T_dog;
var count;
proc print;
run;
REMEMBER: Transposing a file means...
/*** Create datafile T_dog by transposing the variable (column) 'count' in the dog datafile and labed the new columns with the corresponding state **/
proc transpose data = dog out = T_dog;
var count;
id state;
proc print;
run;
/** Add a column titled species to the datafile T_dog and print **/
data T_dog;
set T_dog;
species = 'dog';
proc print;
run;
/** create a datafile bird using species2 containing rows (records) where species equals bird and print ***/
data bird;
set species2;
where species = 'bird';
proc print;
run;
/*** Create datafile T_bird by transposing the variable (column) 'count' in the bird datafile and labed the new columns with the corresponding state **/
proc transpose data = bird out = T_bird;
var count;
id state;
proc print;
run;
/** Add a column titled species to the datafile T_bird and print **/
data T_bird;
set T_bird;
species = 'bird';
proc print;
run;
/** add the data from T_dog to T_bird using the append procedure and print */
proc append base = T_bird data = T_dog;
proc print;
run;
/** create a datafile for each state using species2 data ***/
data AL; /* Alabama data
*/
set species2;
where state = 'AL';
data FL; /* Florida data
*/
set species2;
where state = 'FL';
/* Georgia data */
data GA;
set species2;
where state = 'GA';
/* South Carolina data */
data SC;
set species2;
where state = 'SC';
run;
/*** Create a new datafile for each state by transposing the variable (column) 'count' in the corresponding state datafile and labeled the new columns with the species **/
/*** Alabama *****/
proc transpose data = AL out = T_AL;
var count;
id species;
data T_AL;
set T_AL;
state = 'AL';
/*** Florida *****/
proc transpose data = FL out = T_FL;
var count;
id species;
data T_FL;
set T_FL;
state = 'FL';
/*** Georgia *****/
proc transpose data = GA out = T_GA;
var count;
id species;
data T_GA;
set T_GA;
state = 'GA';
/*** South Carolina *****/
proc transpose data = SC out = T_SC;
var count;
id species;
data T_SC;
set T_SC;
state = 'SC';
run;
/**** Put the transposed state files together using 'proc append'. Use Florida for the base because it has all of the species and print **/
proc append base = T_FL data = T_AL;
proc append base = T_FL data = T_GA;
proc append base = T_FL data = T_SC;
proc print;
run;
/*** Create a new datafile T_species by transposing the variable (column) 'count' in species2 by state and labed the new columns with the species **/
proc sort data = species2; by state; /**
MUST SORT FIRST BY STATE **/
proc transpose data = species2 out = T_species;
by state;
var count;
id species;
proc print;
run;
/* fill in the blank values in T_species with zero's and print */
data T_species;
set T_species;
if dog = . then dog = 0;
if cat = . then cat = 0;
if hog = . then hog = 0;
if eel = . then eel = 0;
if deer = . then deer = 0;
if gator = . then gator = 0;
if lizard = . then lizard = 0;
if shark = . then shark = 0;
drop _name_ _label_;
proc print;
run;
data T_species;
set T_species;
richness = 0;
if dog > 0 then richness = richness + 1;
if cat > 0 then richness = richness + 1;
if hog > 0 then richness = richness + 1;
if eel > 0 then richness = richness + 1;
if deer > 0 then richness = richness + 1;
if gator > 0 then richness = richness + 1;
if lizard > 0 then richness = richness + 1;
if shark > 0 then richness = richness + 1;
if bird > 0 then richness = richness + 1;
if fish > 0 then richness = richness + 1;
proc print;
run;