Sie sind auf Seite 1von 8

CHAPTER 1

/* file chap01.sas */
/* Set up Information

*/

/* This code assumes that the SAS data sets for


Principles of Econometics 4e are in the default
directory. */
/* Data sets can be downloaded from
http://www.principlesofeconometrics.com/poe4/poe4sas.htm
See http://www.principlesofeconometrics.com/poe4/usingsas.htm
for SAS code for all chapters in Principles of Econometrics, 4e
Hill, Griffiths and Lim (2011), John Wiley & Sons, Inc.
Copyright C 2012 by R. Carter Hill and Randall C. Campbell
used for "Using SAS for Econometrics"
by R. Carter Hill and Randall C. Campbell (2012)
John Wiley and Sons, Inc. */
options nodate nonumber linesize=78;
/*------------------------------------*/
/* Styles of comments in SAS programs */
/*------------------------------------*/
* this is a legal comment statement;
* a comment statement can stretch over one
or more lines as long as it ends in a semi-colon;
/* the second type of comment looks like this */
/* this type of comment can also stretch over more
than one line. It can be used to COMMENT OUT
portions of code that you do not wish executed.
This is shown in the next comment. */
/* proc print; run; */
/* this type of comment can include
a *commment statement; */
data htwt;
input name $ sex $ age height weight;
x = height + weight;
/* next statements
datalines;
alfred
M 14 69
alice
F 13 56
barbara
F 14 62
henry
M 15 67

define data lines */


112
84
102
135

* name data set;


* specify variables;
* define variable;

john
sally
;
run;

M 16 70 165
F 16 63 120

/* print data */
proc print data=htwt;
run;

* print to Output;

/* SAS uses MOST RECENTLY CREATED data set if data= omitted */


proc print;
title 'Height-Weight Data';
* add title;
run;
/* Print just some variables using VAR statement */
proc print data=htwt;
var name sex age;
* specify variable to print;
title;
* turn title off;
run;
/* Print the first 3 observations */
proc print data=htwt(obs=3);
run;
/* Print observations 2-4 */
proc print data=htwt(firstobs=2 obs=4);
run;
/* summary statistics */
proc means data=htwt;
run;
/* summary stats of most recently created data set */
/* for selected variables using VAR statement
*/
proc means;
* proc means last data;
var age height weight;
* specify variables;
run;
/* summary stats with 3 decimal places */
proc means data=htwt maxdec=3;
* set maximum decimals;
var age height weight;
* specify variables;
run;
/* make a spelling error -- check Log */
proc means data=hwtt;
run;
/* Using semi-colon to put several commands on one line */
proc means data=htwt; run;
/* make an error--omit a semi-colon. Check Log */
proc means data=htwt run;
/* simple plot in Output */
proc plot data=htwt;
plot weight*height;
run;

* PROC PLOT;
* plot y against x;

/* high quality graphics */


proc gplot data=htwt;
plot weight*height;
run;

* PROC GPLOT;
* plot y against x;

/* use symbol statement to alter appearance */


symbol1 value=dot;
proc gplot data=htwt;
plot weight*height;
run;
/*---------------------------*/
/* following assumes data set*/
/*
htwt is in memory
*/
/*---------------------------*/
/* creating a new data set */
data htwt2;
set htwt;
/* more statements can go here */
run;
/* modifying an existing data set */
data htwt;
set htwt;
/* more statements can go here */
run;
/* using keep statment */
data htwt2;
set htwt;
keep name sex;
proc print data=htwt2;
run;
/* using drop statement */
data htwt2;
set htwt;
drop x height weight;
proc print data=htwt2;
run;
/* arithmetic operations */
data arith;
set htwt;
sum = height + weight;
diff = height - weight;
prod = height * weight;
div = height / weight;
power = age**2;
sqrt = age**.5;
recip = age**-1;
negage = -age;
negage2 = -age*2;
complex = ((height + weight)/age)**2;
run;

* creates a new data set;


* read HTWT;

* open HTWT;
* read HTWT;

* KEEP statement;

* DROP statement;

* new data set;


* read HTWT;
* sum;
* difference;
* product;
* division;
* age squared;
* power 1/2;
* power -1;
* minus age;
* minus age times 2;
* inner paren first;

/* PROC PRINT with VAR option */


proc print data=arith;
var name age height weight sum diff prod div power sqrt recip negage negage2
complex;
run;
/* comparison operators */
data compare;
set htwt;
female = (sex='F');
sixteen = (age=16);
male = (female ^= 1);
older = (age >=16);
run;

* new data set;


* set htwt;
* equal;
* equal;
* not equal;
* greater than or equal;

proc print data=compare;


var name age female sixteen male older;
run;
/* logical operators */
data logical;
set compare;
oldermale = (sex='M' and age>=16);
oldrfem = (female=1 or older=1);
run;

* new data set;


* open compare;
* use of AND;
* use of OR;

proc print data=logical;


var name age oldermale oldrfem;
run;
/* functions */
data function;
set arith;
lage = log(age);
rootage = sqrt(age);
expage = exp(age/10);

*
*
*
*

* new data set;


read arith;
natural log;
square root;
exponential function;

/* note: when using exponential function values can get large


so above we divide by 10 first */
roundx = round(complex);
floorx = floor(complex);
ceilx = ceil(complex);
absx = abs(complex);
run;

* round to nearest integer;


* round down to integer;
* round up to integer;
* absolute value;

proc print data=function;


var name age complex lage rootage expage roundx floorx ceilx absx;
run;
/* Illegal operations cause MISSING VALUES */
data missing;
* new data set;
set htwt;
* read htwt;
female = (sex = 'F');
* female;
lsex = log(female);
* log(0) is illegal;
run;

proc print data=missing;


var name female lsex;
run;
/* Recoding variables with IF-THEN statement */
data htwt2;
* new data set;
set htwt;
* open htwt;
if weight < 110 then wtgrp = 1;
* group 1;
if 110 <= weight < 130 then wtgrp = 2;
* group 2;
if weight >= 130 then wtgrp = 3;
* group 3;
proc print data=htwt2;
var name weight wtgrp;
run;
/* Creating subset of data with SET statement */
data male;
* new data set;
set htwt;
* read htwt;
if sex = 'M';
* subsetting IF;
proc print data=male;
run;
/* Using SET to combine two similar data sets */
data female;
* new data set;
set htwt;
* open htwt;
if sex = 'F';
* keep females;
data all;
set male female;

* new data set;


* Use SET to combine;

proc print data=all;


run;
/* Using SET to open a SAS data set */
/* Assumes default directory set to location of data sets */
data food;
* new data set;
set 'food';
* read sas dataset;
proc contents data=food position;
* examine contents;
proc means data=food;
* summary stats & labels;
run;
/* Using SAS SYSTEM OPTIONS */
options nolabel;
proc means data=food;
run;
options label;
proc means data=food;
run;
/* Adding LABELS */
data htwt2;
set htwt;
label name = 'teenager name'
sex = 'teen gender'

* turn labels off;


* summary stats nolabels;
* turn labels on;
* summary stats & labels;

age = 'age in years'


height = 'height in inches'
weight = 'weight in pounds';

drop x;
proc means data=htwt2 maxdec=3;
run;

/* Use PROC SORT to sort data set */


proc sort data=htwt;
by sex;
proc print data=htwt;
run;

* drop x from data set;


* summary stats with labels;

* sorted data;
* sorting variable;

/* Sorted data can be printed using BY */


proc print data=htwt;
by sex;
run;
/* Sorted data summary stats using BY */
proc means data=htwt;
by sex;
run;
/* Sort by sex and height */
proc sort data=htwt;
by sex height;
proc print data=htwt;
run;

* sorting variables;

/* Sort first on sex and then on height in descending order */


proc sort data=htwt;
by sex descending height;
proc print data=htwt;
run;
/* Using MERGE to combine data sets */
/* First put data in original order--sort by name */
proc sort data=htwt;
* put into original order;
by name;
* sort by name;
run;
/* use automatic variable _N_ = observation number */
data htwt2;
* new data set;
set htwt;
* read HTWT;
id = _N_;
* create id variable;
proc print data=htwt2;
run;
/* new data set containing student id and test score */
data test;
* data sorted on score;
input id score;
* input id and score;
datalines;
6 99
4 92
5 88
3 82

1 72
2 69
;
proc sort data=test;
by id;
proc print data=test;
run;
data test;
merge test htwt2;
by id;
proc print data=test;
run;

* sort data ascending order;


* sort on variable id;

* merge combines data sets;


* BY indicates match variable;

/* Appendix 1B */
options label nodate nonumber linesize=78;
/* Create a new SAS data set */
data htwt;
input name $ sex $ age height weight;
datalines;
alfred M 14 69 112
alice F 13 56 84
barbara F 14 62 102
henry M 15 67 135
john M 16 70 165
sally F 16 63 120
;
run;
/* Read in an external ASCII file brumm.dat */
/* Add labels
*/
data brumm;
infile 'brumm.dat';
input money inflation growth initial poprate inv school;
label money = 'growth rate of money supply'
inflation = 'growth rate of prices'
growth = 'growth rate of output'
initial = 'initial level of GDP per capita'
poprate = 'average population growth rate'
inv = 'average investment share of GDP'
school = 'educational attainment';
run;
proc contents data=brumm position;
run;
/* Save SAS dataset brumm.sas7bdat */
data 'brummdata';
set brumm;
run;
/* Read existing SAS dataset c:\data\poe4sas\cps.sas7bdat */
data cps;
set 'cps';
run;

/*-----------------------------------------------*/
/* Example 4: Import stata data set using wizard */
/* SAS will generate code for future use
*/
/*-----------------------------------------------*/
PROC IMPORT OUT= POE4SAS.SIRMANSDATA
DATAFILE= "C:\data\poe4sas\sirmans.xlsx"
DBMS=EXCEL REPLACE;
RANGE="Data$";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
proc contents data=poe4sas.sirmansdata;
RUN;

Das könnte Ihnen auch gefallen