SAS Ex
SAS Ex
libname in 'g:\stats_data\st553\sasdata';
DATA scores;
SET in.classscores;
RUN;
DATA toads;
INFILE "g:\stats_data\st553\ascii_data\ToadJump.dat";
INPUT ToadName $ Weight Jump1 Jump2 Jump3;
* Print the data to make sure the file was read correctly;
**********************************************************************;
b. Column input
DATA sales;
INFILE "g:\stats_data\st553\ascii_data\Onions.dat";
INPUT VisitingTeam $ 1-20 ConcessionSales 21-24 BleacherSales 25-28
OurHits 29-31 TheirHits 32-34 OurRuns 35-37 TheirRuns 38-40;
* Print the data to make sure the file was read correctly;
DATA contest;
INFILE "g:\stats_data\st553\ascii_data\Pumpkin.dat";
INPUT Name $16. Age 3. +1 Type $1. +1 Date mmddyy10.
(Score1 Score2 Score3 Score4 Score5) (4.1);
* Print the data set to make sure the file was read correctly;
d. Mixed input
DATA nationalparks;
INFILE "g:\stats_data\st553\ascii_data\Park.dat";
INPUT ParkName $ 1-22 State $ Year @40 Acreage COMMA9.;
DATA sales;
INPUT name $ Class DateReturned mmddyy10. CandyType$ Quantity;
CARDS;
Adriana 21 3/21/2000 MP 7
Nathan 14 3.21.2000 CD 19
Matthew 14 3.21.2000 CD 14
Claire 14 3.22.2000 CD 11
Caitlin 21 3.24.2000 CD 9
Ian 21 3.24.2000 MP 18
Chris 14 3.25.2000 MP 6
Anthony 21 3.25.2000 MP 13
Stephen 14 3.25.2000 CD 10
Erika 21 3.25.2000 MP 17
;
PROC PRINT;
RUN;
*****************************************************************************;
DATA flowers;
INPUT CustomerID$ SaleDate mmddyy10. Petunia SnapDragon Marigold;
DATALINES;
756-01 05.04.2001 120 80 110
834-01 05.12.2001 90 160 60
901-02 05.18.2001 50 100 75
834-01 06.01.2001 80 60 100
756-01 06.11.2001 100 160 75
901-02 06.19.2001 60 60 60
756-01 06.25.2001 85 110 100
;
PROC PRINT;
RUN;
*****************************************************************************;
DATA coffee;
INPUT coffee $ window $ @@;
CARDS;
esp w cap d cap w kon w ice w kon d esp d kon w ice d esp d
cap w esp d cap d Kon d . d kon w esp d cap w ice w kon w
kon w kon w ice d esp d kon w esp d esp w kon w cap w kon w
;
PROC PRINT;
RUN;
*****************************************************************************;
DATA ClassScores;
INPUT score @@;
DATALINES;
56 78 84 73 90 44 76 87 92 75 85 67 90 84 74 64 73 78 69 56 87 73 100 54 81 78 69
64 73 65
;
PROC PRINT;
RUN;
DATA plants.magnolia;
INFILE "g:\stats_data\st553\ascii_data\Mag.dat";
INPUT ScientificName $ 1-14 CommonName $ 16-32 MaximumHeight
AgeBloom Type $ Color $;
RUN;
*****************************************************************************;
LIBNAME in "z:\st553\sasdata";
DATA in.homegarden;
INFILE "g:\stats_data\st553\ascii_data\Garden.dat";
INPUT Name $ 1-7 Tomato Zucchini Peas Grapes;
PROC PRINT DATA=in.homegarden;
TITLE "Home Gardening Survey";
RUN;
1. Assignments
LIBNAME in "g:\stats_data\st553\sasdata";
DATA homegarden2;
set in.homegarden;
Zone = 14;
Type = "home";
Zucchini = Zucchini*10;
Total = Tomato + Zucchini + Peas + Grapes;
PerTom = (Tomato / Total) * 100;
RUN;
2. Functions
LIBNAME in "g:\stats_data\st553\sasdata";
DATA contest;
set in.contest;
AvgScore = MEAN(Score1, score2, score3, score4, score5);
AvgScore2 = MEAN(of Score1-score5);
DayEntered = DAY(Date);
Type = UPCASE(Type);
JudgeDate = mdy(07, 04, 2000);
JudgeDate2 = '04jul2000'd;
run;
data grades;
input id section score @@;
if score ge 90 then grade = "A";
else if score ge 80 then grade = "B";
else if score ge 70 then grade = "C";
else if grade ge 60 then grade = "D";
else if score ne . then grade = "F";
select;
when (score ge 93) plusminus = "A";
when (score ge 90) plusminus = "A-";
when (score ge 88) plusminus = "B+";
when (score ge 83) plusminus = "B";
when (score ge 80) plusminus = "B-";
when (score ge 78) plusminus = "C+";
when (score ge 73) plusminus = "C";
when (score ge 70) plusminus = "C-";
when (score ge 68) plusminus = "D+";
when (score ge 63) plusminus = "D";
when (score ge 60) plusminus = "D-";
when (score ne . ) plusminus = "F";
otherwise;
end;
label id = "Student ID number"
section = "Class section number"
score = "Exam score"
grade = "Traditional grade"
plusminus = "Plus/Minus grade"
;
datalines;
811 1 85 138 1 95 137 1 75 642 1 94 134 1 88 466 1 84 258 1 36 733 1 86 844 1 69
131 2 84 336 2 76 541 2 79 951 2 79 348 2 94 846 2 64 187 2 96 976 2 68 199 2 46
879 3 54 796 3 97 872 3 94 647 3 99 994 3 46 884 3 86 946 3 76 465 3 79 944 3 84
;
****************************************************************;
data classdata;
input section score @@;
section1 = 0; section2 = 0; section3 = 0; section4 = 0;
select (section);
when (1) section1 = 1;
when (2) section2 = 1;
when (3) section3 = 1;
when (4) section4 = 1;
otherwise put section=;
end;
datalines;
1 58 1 36 1 87 1 78 1 89 1 67
2 76 2 94 2 64 2 84 2 96 2 47
3 75 3 74 3 86 3 91 3 76 3 84
4 81 4 67 4 97 4 31 4 89 4 67
;
4. Subsetting data
data grades01;
set grades;
if section = 1;
proc print;
run;
********************;
data grades0102;
set grades;
if section in (1,2);
proc print;
run;
*********************;
data grades0102;
set grades;
if section = 3 then delete;
proc print;
run;
*********************;
data grades1AB;
set grades;
where section = 1 and (grade = "A" or grade = "B");
proc print;
run;
5. DO-WHILE loop
data classes;
LENGTH ClassList $60;
ClassNumber = "ST100"; ClassList = "Jim Johnson, Sally Ryan"; output;
ClassNumber = "ST101"; ClassList = "Bob Smith, Ally Carson, Doug Anderson";
output;
ClassNumber = "ST102"; ClassList = "Pete Billingston"; output;
ClassNumber = "ST103"; ClassList = "John Carpenter, Michelle Dante"; output;
ClassNumber = "ST104"; ClassList = "Allison Trenton, Melissa Fredrickson, Jon
Larson"; output;
ClassNumber = "ST105"; ClassList = "Jill North"; output;
DATA data1;
SET classes;
TempList = ClassList;
comma = index(TempList, ",");
DO WHILE (comma gt 0);
student = substr(TempList, 1, comma-1);
output;
TempList = substr(TempList, comma+2, length(TempList)-comma-1);
comma = index(TempList, ",");
END;
student = TempList;
OUTPUT;
DROP TempList comma ClassList;
RUN;
data classes;
LENGTH ClassList $60;
ClassNumber = "ST100"; ClassList = "Jim Johnson, Sally Ryan"; output;
ClassNumber = "ST101"; ClassList = "Bob Smith, Ally Carson, Doug Anderson";
output;
ClassNumber = "ST102"; ClassList = "Pete Billingston"; output;
ClassNumber = "ST103"; ClassList = "John Carpenter, Michelle Dante"; output;
ClassNumber = "ST104"; ClassList = "Allison Trenton, Melissa Fredrickson, Jon
Larson"; output;
ClassNumber = "ST105"; ClassList = "Jill North"; output;
DATA data1;
SET classes;
TempList = ClassList;
DO UNTIL (comma = 0);
comma = index(TempList, ",");
if comma = 0 then student = TempList;
else student = substr(TempList, 1, comma-1);
output;
TempList = substr(TempList, comma+2, length(TempList)-comma-1);
END;
DROP TempList comma ClassList;
RUN;
SAS Procedures
libname in "g:\stats_data\st553\sasdata";
data sales;
set in.sales;
run;
********************************;
********************************;
proc print data=sales;
by class;
id class;
sum profit;
var name datereturned candytype profit;
title "Candy Sales for Field Trip by Class -- with ID";
run;
libname in 'g:\stats_data\st553\sasdata';
data sales;
set in.flowers;
run;
run;
3.PROC FREQ
libname in 'g:\stats_data\st553\sasdata';
data orders;
set in.coffee;
****;
proc freq;
tables window*coffee / missprint;
tables window*coffee / missing;
run;
****;
data orders;
set in.coffee;
coffee = upcase(coffee);
run;
4. PROC UNIVARIATE
libname in 'g:\stats_data\st553\sasdata';
data class;
set in.classscores;
*****;
*****;
*****;
5. PROC REG
6. PROC GLM
7. PROC GPLOT
data plotdata;
do x = 1 to 100;
y = ranuni(-1);
output;
end;
proc print;
run;
--------------------------------------------- Ex ---------------------------------------------
Session 1
You can select and copy each one of the following examples and paste them directly into your SAS Editor window. They
should each run without errors.
[edit]
Example 1
data test;
input x y;
cards;
1 2
3 4
5 80
;
run;
← Notice that a dataset is created, but no output is produced. What's missing from the program?
After the line that says 5 80 add a new line that says
6 .
Run the whole program again. Notice that SAS does not produce an error message.
[edit]
Example 2
Taken from the SAS Online Documentation, Example from the PROC REG procedure.
data fitness;
input Age Weight Oxygen RunTime RestPulse RunPulse MaxPulse @@;
datalines;
44 89.47 44.609 11.37 62 178 182 40 75.07 45.313 10.07 62 185 185
44 85.84 54.297 8.65 45 156 168 42 68.15 59.571 8.17 40 166 172
38 89.02 49.874 9.22 55 178 180 47 77.45 44.811 11.63 58 176 176
40 75.98 45.681 11.95 70 176 180 43 81.19 49.091 10.85 64 162 170
44 81.42 39.442 13.08 63 174 176 38 81.87 60.055 8.63 48 170 186
44 73.03 50.541 10.13 45 168 168 45 87.66 37.388 14.03 56 186 192
45 66.45 44.754 11.12 51 176 176 47 79.15 47.273 10.60 47 162 164
54 83.12 51.855 10.33 50 166 170 49 81.42 49.156 8.95 44 180 185
51 69.63 40.836 10.95 57 168 172 51 77.91 46.672 10.00 48 162 168
48 91.63 46.774 10.25 48 162 164 49 73.37 50.388 10.08 67 168 168
57 73.37 39.407 12.63 58 174 176 54 79.38 46.080 11.17 62 156 165
52 76.32 45.441 9.63 48 164 166 50 70.87 54.625 8.92 48 146 155
51 67.25 45.118 11.08 48 172 172 54 91.63 39.203 12.88 44 168 172
51 73.71 45.790 10.47 59 186 188 57 59.08 50.545 9.93 49 148 155
49 76.32 48.673 9.40 56 186 188 48 61.24 47.920 11.50 52 170 176
52 82.78 47.467 10.50 53 170 172
;
run;
proc reg data=fitness;
model Oxygen=RunTime;
run;
[edit]
Session 2
To understand the differences in the SAS Statments-INPUT and INFILE, See the following examples.
[edit]
Example 1
The INFILE statement is to identify an external file.
[edit]
Example 2
[edit]
Example 3
Situation: When you have 1000000 observations in your data set, and you want to take a
look
at it without reading the entire data file. You can add OBS=n to the INFILE
satement, so that you can process only records 1 through n.
FILENAME CAT 'C:\USERS\CAT.DAT';
DATA PETS;
INFILE CAT OBS=10;
INPUT ID $ 1-4 AGE 6-7 SEX $ 8;
RUN;
Notice that you only run the first ten observations here.
[edit]
Example 4
The data statements of this example make use of a new feature called direct referencing (version 9.1). By using this new
feature we avoid the added step of using a FILENAME statement.
Note: this example won't run without errors since in class we used File Import in the SAS menu to create a WORK.Class1
dataset.
Here is the raw text that belongs in a file called class1.txt in the C:\Temp folder (from an Excel spreadsheet).
id age name
1 19George
2 20Mary
3 21Xena
4 21Juan
data class2;
infile 'c:\Temp\class1.txt' firstobs=2 truncover;
input id 1-8 age 9-16 name $ 17-24;
run;
data 'c:\Temp\plato.sas7bdat';
set class1;
run;
data test;
input x y;
cards;
1 2
3 4
5 80
6 16000
;
run;
data 'c:\Temp\test.sas7bdat';
set test;
run;
[edit]
Example 5
data test;
input x y z;
cards;
1 2 4
3 4 7
5 80 3
6 20 2
9 30 1
;
run;
Run the whole example in your computer and notice that you only print out the last four observations in output.
[edit]
Example 6
MISSOVER: To prevent an INPUT statement from reading a new input data record if it does not find values in the current
input line for all the variables in the statement. --from SDLEo
data scores;
infile datalines missover;
input score1-score5;
datalines;
90 98 98
80 100 98 70 78
20 50 90 30 60
;
run;
proc print data=scores;
run;
Run the above program and notice the missing value in the ouput window.
[edit]
Example 7
Change the option-MISSOVER to FLOWOVER and TRUNCOVER in the previous example, and see what's the
difference.
[edit]
Session 3
[edit]
Example 1
data test;
input f1;
cards;
1
2
3000
0.0004
0.0005
6.6
70
365
366
367
;
run;
data test2;
set test;
f2=f1;
f3=f1;
f4=f1;
f5=f1;
format f2 8.2 ;
format f3 date9. ;
format f4 dollar8.2 ;
format f5 8.0 ;
run;
proc print data=test2;
run;
[edit]
Example 2
Great white sharks (Wikipedia article) are killing machines that have not needed to evolve for millions of years.
An extended example. The data records the average length in feet of selected whales and sharks.
data sealife;
input name $ family $ length ;
datalines;
beluga whale 15
whale shark 40
basking shark 30
gray whale 50
mako shark 12
sperm whale 60
dwarf shark .5
whale shark 40
humpback . 50
blue whale 100
killer whale 30
;
run;
[edit]
Example 3
Using the fitness example from the earlier session we looked at a common usage for PROC SORT
data fit2;
set fitness;
* Here's a simple calculation that will give us two age groups;
agecat=1; /* The 'young' age category */
if (age>50) then agecat=2; /* 'old' */
run;
[edit]
Example 4
Candy sales data, similar to the data mentioned in The Little SAS Book, Section 4.4 (light blue edition, page 107).
Briefly editting the text above we wrote the program to illustrate two ways to use PROC FREQ.
data candy;
input Name $ ClassRm Month Day Year Candy $ Quantity ;
cards;
Adriana 21 3 2 2000 MP 7
Nathan 14 2 28 2000 CD 19
Matthew 14 3 1 2000 CD 14
Claire 14 3 3 2000 CD 11
Caitlin 21 2 24 2000 CD 9
Ian 21 3 3 2000 MP 18
Chris 14 2 18 2000 CD 6
Anthony 21 6 1 2000 MP 13
Stephen 14 3 25 2000 CD 10
Erika 21 3 25 2000 MP 17
;
run;
[edit]
Session 4
[edit]
Examples (part 1)
data htwt;
input Name $ 1-10 Sex $ 12 Age 14-15 Height 17-18 Weight 20-22;
datalines;
ALFRED M 14 69 112
ALICE F 13 56 84
BARBARA F 14 62 102
BERNADETTE F 13 65 98
HENRY M 14 63 102
JAMES M 12 57 83
JANE F 12 59 84
JANET F 15 62 112
JEFFREY M 13 62 84
JOHN M 12 59 99
JOYCE F 11 51 50
JUDY F 14 64 90
LOUISE F 12 56 77
MARY F 15 66 112
PHILLIP M 16 72 150
ROBERT M 12 64 128
RONALD M 15 67 133
THOMAS M 11 57 85
WILLIAM M 15 66 112
;
run;
Notice the error message that occurs when there is no active Output Destination (above).
The following is a simple, very controlled way of directing where the output from a procedure should go ... and in what
format you'd like to make the output. Other popular choices for output formats are:
ODS and function examples using Tomato data (from the Little SAS Book, 3rd edition, Section 5.3)
Now, on to limiting the observations output by a datastep, functions and creating new variables.
data tom2;
set tomatoes;
if (Days>80);
*if (Days <= 80) then delete;
run;
data tom3;
set tomatoes;
newsum=Days+Weight;
r=round(Weight);
first3=substr(Name,1,3);
spacepos=index(name,' ');
caps=upcase(Name);
leng=length(Name);
run;
[edit]
Example 2 (skipped)
The following was not covered in class due to time contraints. The data is derived from the Little SAS Book, 3rd edition,
Section 5.11. (I was lazy about typing last names. --Rhansen 13:27, 28 September 2006 (EDT)) Traffic-Lighting Data
[edit]
The following is not a proper reading of data mention in both editions of The Little SAS Book (2nd and 3rd editions) in the
chapter about Simple Regression analyses. To illustrate a practicle use of arrays we've pretended that each T-ball player
tries to hit a ball five times, rather than treating all 30 height/distance pairs as separate players.
data baseball;
input Height1 Distance1
Height2 Distance2
Height3 Distance3
Height4 Distance4
Height5 Distance5;
cards;
50 110 49 135 48 129 53 150 48 124
50 143 51 126 45 107 53 146 50 154
47 136 52 144 47 124 50 133 50 128
50 118 48 135 47 129 45 126 48 118
45 121 53 142 46 122 47 119 51 134
49 130 46 132 51 144 50 132 50 131
;
run;
data bb2;
set baseball;
array H {5} Height1 Height2 Height3 Height4 Height5;
do i=1 to 5;
H{i}=H{i}*2.54; /* To convert inches to centimeters */
end;
run;
data simple;
input x;
cards;
1
2
5
6
;
run;
data newsimp;
set simple;
do i=1 to 3;
output;
end;
run;
[edit]
Example 4
COMPANY = Mutual Fund Company
RETURN = Average Annual Return, Percent
SDAR = Standard Deviation of Annual Return, Percent
Name Return SDAR
Group Securities. Common Stock Fund 15.1 19.1
Incorporated Investors 14.0 25.5
Investment Company of America 17.4 21.8
Investors Mutual 11.3 12.5
Loomis-Sales Mutual Fund 10.0 10.4
Massachusetts Investors Trust 16.2 20.8
National Investors Corporation 18.3 19.9
National Securities-Income Series 12.4 17.8
New England Fund 10.4 10.2
Massachusetts Investors-Growth Stock 18.6 22.7
Group Securities. Fully Administered Fund 11.4 14.1
[edit]
Example 5
Time:Total minutes spent on homework per week.
Days:Days spent on homework per week.
Time Days
100 3
150 4
99 5
. 6
160 3
80 .
200 3
[edit]
Example 6
FIELD = Field of Specialization
WOMEN = Median Salaries of Women, Thousands of $
MEN = Median Salaries of Men, Thousands of $
FIELD WOMEN MEN
Business, Finance, Etc. 9.3 13.0
Labor Economics 10.3 12.0
Monetary-Fiscal 8.0 11.6
General Economic Theory 8.7 10.8
Population, Welfare Programs, Etc. 12.0 11.5
Economic Systems and Development 9.0 12.2
[edit]
Session 5
[edit]
Example 1
data test;
x=1;
a=2;
b=3;
c=18;
run;
data test2;
set test;
y=32;
d=a+b;
run;
data test3;
set test2;
output;
output;
output;
run;
[edit]
Example 2
data alumni;
year=1995;
contrib=1000;
output;
year=year+5;
contrib=1500;
output;
year=year+5;
output;
year=year+5;
output;
run;
data alumni;
year=1995;
output;
year=year+5;
contrib=1500;
output;
year=year+5;
output;
year=year+5;
output;
run;
data alumni50;
year=1995;
contrib=1000;
do i=1 to 10;
contrib=contrib+25;
year=year+5;
output;
end;
run;
data pop;
year=1790;
output;
do i=1 to 21;
year=year+10;
output;
end;
run;
[edit]
Example 2a
Combining a sequence with real data of US Population estimates starting in 1790 (Data from the SAS online
documentation PROC REG (regression) overview.)
data actpop;
input population;
cards;
3.929
5.308
7.239
9.638
12.866
17.069
23.191
31.443
39.818
50.155
62.947
75.994
91.972
105.71
122.775
131.669
151.325
179.323
203.211
226.542
248.71
281.422
;
run;
data fullpop;
merge pop actpop;
run;
proc print data=fullpop;
run;
[edit]
Example 2b
Using the population data for a quick linear regression. Actually looking at the plot reveals what a poor fit the linear model
is.
[edit]
Example 3
Heights, Weights and Ages for a class.
data class;
input Name $ Height Weight Age @@;
datalines;
Alfred 69.0 112.5 14 Alice 56.5 84.0 13 Barbara 65.3 98.0 13
Carol 62.8 102.5 14 Henry 63.5 102.5 14 James 57.3 83.0 12
Jane 59.8 84.5 12 Janet 62.5 112.5 15 Jeffrey 62.5 84.0 13
John 59.0 99.5 12 Joyce 51.3 50.5 11 Judy 64.3 90.0 14
Louise 56.3 77.0 12 Mary 66.5 112.0 15 Philip 72.0 150.0 16
Robert 64.8 128.0 12 Ronald 67.0 133.0 15 Thomas 57.5 85.0 11
William 66.5 112.0 15
;
run;
[edit]
Example 4
data class2;
set class;
group=1;
if substr(Name,1,1)='J' then group=2;
run;
[edit]
Example 4a
Illustrating an efficient concise form for outputing several regression models' results
[edit]
Example 5
Illustrating using PROC REG to generate other output; the residuals from a regression
proc reg data=class2;
model height=age;
output out=DS1 r=george;
run;
Left as an exercise: If we want the largest outlier (not the most positive), how should we change the program above?
What should we do with the dataset DS1?
Session 6
[edit]
Example 1
data test;
file 'c:\Temp\myfile.txt';
x=10; y=20;
put x;
put y;
output;
x=3; y=36;
put x;
put y;
put 'All done.';
run;
Try putting a * in front of the file statement. Run the code clip again and look what's in the LOG.
After making a dataset in SAS, it becomes a temporary .sas7bdat file in a WORK library that is cleared when you close
the SAS session.
data test;
x=10; y=20;
output;
run;
Before you close SAS you can simple read it by using a set statement.
data testds;
infile 'c:\Temp\myfile.txt';
input x;
run;
[edit]
Example 2
Strings
data strings1;
infile 'c:\Temp\strings.txt';
input id month $ grade $1. correct total;
run;
Without an informat specification on the INPUT statement SAS may have some difficulty determining what length to make
the placeholder for the textual information in a column.
data strings1;
infile 'c:\Temp\strings.txt';
input id month $ grade $ correct total;
run;
To assist SAS with correctly jumping to a column where we expect certain data to reside on each record/row we use
pointer control (using the @ sign on the INPUT statement).
data justyr;
infile 'c:\Temp\strings.txt';
input @10 yr @13 grade $1. @15 correct total;
run;
[edit]
Example 3
data strings2;
input id month $ grade $upcase1. correct total major $20.;
cards;
101 10-2006 A 12 14 Economics
101 11-2006 A 15 15 Econ
101 12-2006 B 15 20 economics
101 1-2007 a 15 15 econ
102 10-2006 c 10 14 Psychology
102 12-2006 C 12 20 Psych
102 01-2007 B 13 15 PSYCH
103 10-2006 a 14 14 Econ
103 11-2006 B 12 15 Econ
103 12-2006 B 16 20 Econ
104 01-2007 A 15 15 Decker School of Nursing
;
run;
[edit]
Example 4
data str4;
set strings2;
maj=upcase(substr(major,1,4));
run;
[edit]
Example 4a
A short practical use of converting everything to upper case and using a 'subsetting IF' statement.
data econ;
set str4;
if (maj='ECON');
run;
/*
index
anylower
anyupper if 'a'='A' then do; end;
anyspace if upcase('a')=upcase('A') then do; end;
*/
[edit]
Example 5
data numbers;
input n 4.1 @6 n2 ;
cards;
012345678910
8912827384
999999
6666 103
;
run;
[edit]
Closing examples
data stringok;
input @21 majorcap $upcase4.;
cards;
101 10-2006 A 12 14 Economics
101 11-2006 A 15 15 Econ
101 12-2006 B 15 20 economics
101 1-2007 a 15 15 econ
102 10-2006 c 10 14 Psychology
102 12-2006 C 12 20 Psych
102 01-2007 B 13 15 PSYCH
103 10-2006 a 14 14 Econ
103 11-2006 B 12 15 Econ
103 12-2006 B 16 20 Econ
104 01-2007 A 15 15 Decker School of Nursing
;
run;
data stringweird;
input @21 majorcap $revers4.;
cards;
101 10-2006 A 12 14 Economics
101 11-2006 A 15 15 Econ
101 12-2006 B 15 20 economics
101 1-2007 a 15 15 econ
102 10-2006 c 10 14 Psychology
102 12-2006 C 12 20 Psych
102 01-2007 B 13 15 PSYCH
103 10-2006 a 14 14 Econ
103 11-2006 B 12 15 Econ
103 12-2006 B 16 20 Econ
104 01-2007 A 15 15 Decker School of Nursing
;
run;
[edit]
Session 7
No data for today. We'll use datasets in the SASHELP library, or files that we explicitly create in class. Be sure to look at
the references (listed at 'Essential Readings') for functions and functions by category (to find those listed for SAS Dates
and Times). We'll be speaking about date/time related Formats, and Informats today as well.
To look at what's possible with SAS graphics the SAS/Graph overview is an excellent place to start.
[edit]
In class
data test;
x=today();
x1=today()+1;
x2='31DEC1960'D ;
x3='02JAN1961'D ;
x4='02JAN1961'D +0;
x5='02JAN1961'D *1;
run;
data test2;
set test;
format x1 MMDDYYS8. ;
format x2 WEEKDATE23. ;
format x3 WEEKDAY1. ;
run;
data test3;
set sashelp.air;
day=day(date);
mo=month(date);
run;
data test4;
dob='07JUL1980'D ;
t=today();
*yearsold=intck(year,dob,t);
yrsapprox=(t-dob)/365;
run;
proc print data=test4;
run;
* DATDIF INTCK;
data test5;
m=6;
d=3;
y=1960;
sasda=mdy(m,d,y);
run;
data class2;
set sashelp.class;
run;
ods html;
ods graphics on;
proc reg data=class2;
model height=weight;
run;
[edit]
[edit]
Session 8
101 57
102 52
104 87
105 69
106 62
107 75
108 41
109 41
110 79
111 25
119 36
118 82
114 41
[edit]
Session 9
[edit]
Example 1
for arrays
Baseball
YANKS,0,0,0,0,1,2,0,0,1
METS, 0,0,0,0,0,1,0,1,1
[edit]
Example 2
Soup data
North,Johnson
City,CHICKRICE,CHICKRICE,CHICKRICE,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICK
NOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKGARL,CHICKGARL,CHICKGARL,POTATOG
ARL,POTATOGARL,TOMATO,TOMATO,TOMATO,TOMATO,TOMATO,TOMATO,TOMATO,CHICKWILD,CHICKWILD,CHICKW
ILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,MINESTRONE,MINESTRONE,LENT
IL,MEATPAST,BEEFBAR,ITALIANWED,CLAMCHOWNE,CLAMCHOWFF,SAUSPEPP,SAUSPEPP,BEANHAM,BEANHAM,BEA
NHAM,SPLITPEAHAM,SPLITPEAHAM,SPLITPEAHAM,,,,,,,
Main,Binghamton,CHICKVEG,CHICKRICE,CHICKRICE,CHICKRICE,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICK
NOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVEGM
ED,CHICKGARL,CHICKGARL,CHICKGARL,POTATOGARL,POTATOGARL,TOMATO,TOMATO,TOMATO,TOMATO,TOMATO,
TOMATO,TOMATO,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,MEATPAST,BEEFBAR
,ITALIANWED,CLAMCHOWNE,CLAMCHOWNE,SAUSPEPP,SAUSPEPP,SAUSPEPP,SAUSPEPP,BEANHAM,BEANHAM,BEAN
HAM,SPLITPEAHAM,SPLITPEAHAM,,,,,,,,,
North,Endwell,CHICKMEX,CHICKMEX,CHICKMEX,CHICKMEX,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,
CHICKNOOD,CHICKNOOD,CHICKRICE,CHICKRICE,CHICKALF,CHICKALF,CHICKALF,CHICKALF,CHICKALF,CHICK
PENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKVEG,CHICKVEGMED,CHICKVEGMED,CHEESER
AV,VEGMED,VEGMED,VEGMED,VEGMED,POTATOGARL,POTATOGARL,TOMATO,TOMATO,TOMATO,TOMATO,MINESTRON
E,POTATOBROCC,POTATOBROCC,POTATOBROCC,LENTIL,MEATPAST,MEATPAST,BEEFVEG,ITALIANWED,ITALIANW
ED,ITALIANWED,CLAMCHOWNE,SAUSPEPP,SAUSPEPP,SAUSPEPP,BEANHAM,BEANHAM,BEANHAM,BEANHAM,SPLITP
EAHAM,
Main,Vestal,CHICKMEX,CHICKMEX,CHICKMEX,CHICKMEX,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CH
ICKNOOD,CHICKNOOD,CHICKRICE,CHICKRICE,CHICKALF,CHICKALF,CHICKALF,CHICKALF,CHICKALF,CHICKPE
NNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKVEG,CHICKVEGMED,CHICKVEGMED,CHEESERAV
,VEGMED,VEGMED,VEGMED,VEGMED,POTATOGARL,POTATOGARL,TOMATO,TOMATO,TOMATO,TOMATO,MINESTRONE,
POTATOBROCC,POTATOBROCC,POTATOBROCC,LENTIL,MEATPAST,MEATPAST,MINESTRONE,MINESTRONE,MINESTR
ONE,TOMATO,TOMATO,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWFF,CLAMCHOWFF,BEANHAM,BEANHAM,BEANHAM,BEAN
HAM,SPLITPEAHAM
Jenson,Binghamton,CHICKRICE,CHICKRICE,CHICKRICE,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CH
ICKNOOD,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKGARL,CHICKGARL,CHIC
KGARL,POTATOGARL,POTATOGARL,TOMATO,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICK
WILD,CHICKWILD,CHICKWILD,CHICKWILD,MINESTRONE,MINESTRONE,LENTIL,MEATPAST,BEEFBAR,ITALIANWE
D,CLAMCHOWNE,CLAMCHOWFF,SAUSPEPP,SAUSPEPP,BEANHAM,BEANHAM,BEANHAM,BEANHAM,SPLITPEAHAM,BEAN
HAM,BEANHAM,BEANHAM,BEANHAM,SPLITPEAHAM,,,,,,,,,
Court,Binghamton,CHICKVEG,CHICKRICE,CHICKRICE,CHICKRICE,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHIC
KNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVEG
MED,CHICKGARL,CHICKGARL,CHICKGARL,POTATOGARL,POTATOGARL,CHICKWILD,CHICKWILD,CHICKWILD,CHEE
SERAV,CHEESERAV,CHEESERAV,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,POTATOBRO
CC,POTATOBROCC,POTATOBROCC,LENTIL,LENTIL,LENTIL,LENTIL,LENTIL,MEATPAST,MEATPAST,BEEFBAR,BE
EFBAR,BEEFBAR,BEEFBAR,CLAMCHOWNE,CLAMCHOWNE,BEANHAM,BEANHAM,BEANHAM,BEANHAM,SPLITPEAHAM,,,
,
Front,Binghamton,LENTIL,LENTIL,LENTIL,BEEFBAR,BEEFBAR,VEGMED,VEGMED,VEGMED,SAUSPEPP,SAUSPE
PP,SAUSPEPP,SAUSPEPP,VEGMED,ITALIANWED,CLAMCHOWNE,CLAMCHOWFF,BEANHAM,BEANHAM,SPLITPEAHAM,C
HICKNOOD,CHICKRICE,CHICKALF,CHICKWILD,CHICKWILD,CHICKPENNE,MEATPAST,MEATPAST,MEATPAST,MINE
STRONE,CHICKALF,VEGMED,VEGMED,POTATOGARL,POTATOGARL,POTATOGARL,,,,,,,,,,,,,,,,,,,,,,
Main,Endicott,CHICKNOOD,CHICKNOOD,CHICKVEGMED,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGME
D,CHICKVEGMED,CHICKGARL,CHICKGARL,CHICKGARL,POTATOGARL,POTATOGARL,CHICKWILD,CHICKWILD,CHIC
KWILD,CHEESERAV,CHEESERAV,CHEESERAV,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE
,POTATOBROCC,POTATOBROCC,POTATOBROCC,LENTIL,LENTIL,LENTIL,LENTIL,LENTIL,TOMATO,TOMATO,MEAT
PAST,MEATPAST,BEEFBAR,BEEFBAR,BEEFBAR,BEEFBAR,CLAMCHOWFF,CLAMCHOWFF,CLAMCHOWNE,CLAMCHOWNE,
BEANHAM,BEANHAM,BEANHAM,BEANHAM,SPLITPEAHAM,,,,,,,,
Grand Ave,Johnson
City,CHICKRICE,CHICKRICE,CHICKRICE,CHICKRICE,CHICKRICE,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED
,CHICKVEGMED,CHICKGARL,CHICKGARL,CHICKGARL,POTATOGARL,POTATOGARL,CHICKWILD,CHICKWILD,CHICK
WILD,CHEESERAV,CHEESERAV,CHEESERAV,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,
POTATOBROCC,POTATOBROCC,POTATOBROCC,LENTIL,LENTIL,LENTIL,LENTIL,LENTIL,MEATPAST,MEATPAST,B
EEFBAR,BEEFBAR,BEEFBAR,BEEFBAR,CLAMCHOWNE,CLAMCHOWNE,BEANHAM,BEANHAM,BEANHAM,BEANHAM,SPLIT
PEAHAM,,,,,,,,,,,
Floral,Johnson
City,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKRICE,CHICKRICE,CHICKALF,CHICKALF,CHICKAL
F,CHICKALF,CHICKALF,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKVEG,CHICKV
EGMED,CHICKVEGMED,CHEESERAV,VEGMED,VEGMED,VEGMED,VEGMED,POTATOGARL,POTATOGARL,TOMATO,TOMAT
O,TOMATO,TOMATO,MINESTRONE,POTATOBROCC,POTATOBROCC,POTATOBROCC,LENTIL,MEATPAST,MEATPAST,MI
NESTRONE,MINESTRONE,MINESTRONE,TOMATO,TOMATO,CLAMCHOWNE,CLAMCHOWNE,TOMATO,,,,,,,,,,,,
Floral,Binghamton,CHICKVEG,CHICKRICE,CHICKRICE,CHICKRICE,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHI
CKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVE
GMED,CHICKGARL,CHICKGARL,CHICKGARL,POTATOGARL,POTATOGARL,TOMATO,TOMATO,TOMATO,TOMATO,TOMAT
O,TOMATO,TOMATO,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,CHICKWILD,MEATPAST,ITALI
ANWED,CLAMCHOWNE,CLAMCHOWNE,SAUSPEPP,SAUSPEPP,SAUSPEPP,SAUSPEPP,BEANHAM,BEANHAM,BEANHAM,SP
LITPEAHAM,SPLITPEAHAM,,,,,,,,,,
Vestal,Binghamton,CHICKMEX,CHICKMEX,CHICKMEX,CHICKMEX,CHICKMEX,CHICKNOOD,CHICKNOOD,CHICKNO
OD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKRICE,CHICKRICE,CHICKALF,CHICKALF,CHICKALF,CHICKALF,C
HICKPENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKVEG,CHICKVEGMED,CHICKVEGMED,CHE
ESERAV,VEGMED,VEGMED,VEGMED,VEGMED,POTATOGARL,POTATOGARL,TOMATO,TOMATO,TOMATO,TOMATO,MINES
TRONE,POTATOBROCC,POTATOBROCC,LENTIL,MEATPAST,MEATPAST,MINESTRONE,MINESTRONE,MINESTRONE,TO
MATO,TOMATO,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,BEANHAM,BEANHAM,BEANHAM,BEANHAM,SP
LITPEAHAM,
West,Endwell,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKGARL,CHICKGARL
,CHICKGARL,POTATOGARL,POTATOGARL,CHICKWILD,CHICKWILD,CHICKWILD,CHEESERAV,CHEESERAV,CHEESER
AV,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,POTATOBROCC,POTATOBROCC,POTATOBR
OCC,LENTIL,LENTIL,LENTIL,LENTIL,LENTIL,TOMATO,TOMATO,MEATPAST,MEATPAST,BEEFBAR,BEEFBAR,BEE
FBAR,BEEFBAR,CLAMCHOWFF,CLAMCHOWFF,CLAMCHOWNE,CLAMCHOWNE,BEANHAM,BEANHAM,BEANHAM,BEANHAM,S
PLITPEAHAM,,,,,,,,,,,
First,Binghamton,CHICKNOOD,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKVEGMED,CHICKGARL,CHICK
GARL,CHICKGARL,POTATOGARL,POTATOGARL,CHICKWILD,CHICKWILD,CHICKWILD,CHEESERAV,CHEESERAV,CHE
ESERAV,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,MINESTRONE,POTATOBROCC,POTATOBROCC,POTA
TOBROCC,LENTIL,LENTIL,LENTIL,LENTIL,LENTIL,MEATPAST,MEATPAST,MEATPAST,MEATPAST,BEEFBAR,BEE
FBAR,BEEFBAR,BEEFBAR,CLAMCHOWFF,CLAMCHOWFF,CLAMCHOWNE,BEANHAM,BEANHAM,BEANHAM,BEANHAM,SPLI
TPEAHAM,TOMATO,,,,,,,,,,,
Farr
Ave,Vestal,VEGMED,VEGMED,VEGMED,TOMATO,TOMATO,CHICKPENNE,CHICKPENNE,CHICKPENNE,CHICKALF,CH
ICKALF,CHICKALF,CHICKALF,CHICKMEX,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKN
OOD,CHICKNOOD,POTATOGARL,LENTIL,LENTIL,LENTIL,LENTIL,LENTIL,BEEFBAR,BEEFBAR,BEEFVEG,BEEFVE
G,BEEFVEG,BEEFVEG,ITALIANWED,ITALIANWED,ITALIANWED,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCH
OWNE,CLAMCHOWNE,CLAMCHOWFF,CLAMCHOWFF,CLAMCHOWFF,CLAMCHOWFF,BEANHAM,BEANHAM,SAUSPEPP,,,,,,
,,,,
Clifford,Binghamton,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,SAUSPEPP,BEANHAM,SPLITPEAHAM,CHICKMEX
,CHICKMEX,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKRICE,CHICKRICE,CHICKRICE,CHICKRICE,
CHICKRICE,CHICKWILD,CHICKWILD,CHICKPENNE,CHEESERAV,CHEESERAV,CHEESERAV,VEGMED,POTATOGARL,M
INESTRONE,MINESTRONE,LENTIL,LENTIL,LENTIL,MEATPAST,BEEFBAR,,,,,,,,,,,,,,,,,,,,,,,,,
Maxwell,Johnson
City,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKVEG,CHICKVEG,CHICKGARL,CHICKGARL,CHEESERAV,CHEESER
AV,CHEESERAV,VEGMED,VEGMED,VEGMED,VEGMED,TOMATO,TOMATO,TOMATO,MINESTRONE,MINESTRONE,POTATO
BROCC,LENTIL,LENTIL,MEATPAST,BEEFBAR,BEEFBAR,BEEFBAR,BEEFBAR,BEEFVEG,BEEFVEG,BEEFVEG,BEEFV
EG,BEEFVEG,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWFF,SAUSPEPP,SAUSPEPP,BEANHA
M,BEANHAM,BEANHAM,SPLITPEAHAM,,
Riverside
Drive,Binghamton,CHICKNOOD,CHICKVEG,CHICKVEG,CHICKGARL,CHICKGARL,CHEESERAV,CHEESERAV,CHEES
ERAV,VEGMED,VEGMED,VEGMED,VEGMED,TOMATO,TOMATO,TOMATO,TOMATO,TOMATO,MINESTRONE,MINESTRONE,
POTATOBROCC,LENTIL,LENTIL,MEATPAST,BEEFBAR,BEEFBAR,BEEFBAR,BEEFBAR,BEEFVEG,BEEFVEG,BEEFVEG
,BEEFVEG,BEEFVEG,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWFF,SAUSPEPP,SAUSPEPP,
BEANHAM,BEANHAM,BEANHAM,SPLITPEAHAM,SPLITPEAHAM,SPLITPEAHAM
Oak,Endicott,CHICKNOOD,CHICKNOOD,CHICKNOOD,CHICKVEG,CHICKVEG,CHICKGARL,CHICKGARL,CHEESERAV
,CHEESERAV,CHEESERAV,VEGMED,VEGMED,VEGMED,VEGMED,TOMATO,TOMATO,TOMATO,MINESTRONE,MINESTRON
E,POTATOBROCC,LENTIL,LENTIL,MEATPAST,BEEFBAR,BEEFBAR,BEEFBAR,BEEFBAR,BEEFVEG,BEEFVEG,BEEFV
EG,BEEFVEG,BEEFVEG,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,CLAMCHOWNE,SAUSPEPP,SAUSPEP
P,BEANHAM,BEANHAM,BEANHAM,,,
[edit]
Example 3
x,y,z
1,2,3
4,5,6
7,8,9
10,11,12
[edit]
Example 3A
id,x,y,z
101,1,2,3
101,4,5,6
101,7,8,9
101,10,11,12
102,10000,20000,30000
102,40000,50000,60000
102,70000,80000,90000
102,99999,99999,99999
[edit]
Example 4
id date returns
[edit]
Session 10
[edit]
Example 1
data score;
input Student $9. StudentID $ Test1 Test2 Final;
datalines;
Capalleti 0545 94 91 87
Dubose 1252 51 65 91
Engles 1167 95 97 97
Grant 1230 63 75 80
Krupski 2527 80 76 71
Lundsford 4860 92 40 86
McBane 0674 75 78 72
;
proc transpose data=score out=scoretran;
run;
proc print data=scoretran;
run;
[edit]
Example 2
Logistic Data
[edit]
pop year
3.929 1790
5.308 1800
7.239 1810
9.638 1820
12.866 1830
17.069 1840
23.191 1850
31.443 1860
39.818 1870
50.155 1880
62.947 1890
75.994 1900
91.972 1910
105.71 1920
122.775 1930
131.669 1940
151.325 1950
179.323 1960
203.211 1970
226.542 1980
248.71 1990
as a csv
pop,year
3.929,1790
5.308,1800
7.239,1810
9.638,1820
12.866,1830
17.069,1840
23.191,1850
31.443,1860
39.818,1870
50.155,1880
62.947,1890
75.994,1900
91.972,1910
105.71,1920
122.775,1930
131.669,1940
151.325,1950
179.323,1960
203.211,1970
226.542,1980
248.71,1990
[edit]