proc print data=test noobs;
The PROC PRINT step prints the data set, which contains one observation and three variables. The NOOBS option suppresses observation numbers in the output.
/*************************************/
/* print the sorted data set */
/*************************************/
proc print data=tempemp split='*';
id jobcode;
by jobcode;
var gender salary;
sum salary;
label jobcode='Job Code*========'
gender='Gender*======'
salary='Annual Salary*=============';
format salary dollar11.2;
where jobcode in ('PT1','PT2');
title 'Expenses Incurred for';
title2 'Salaries for Pilots';
run;
Saturday, March 15, 2008
Thursday, March 13, 2008
Work with SAS dates and times
SAS Date Value - Jan. 1, 1960
SAS stores date value as numeric values while SAS date is stored as numbers.
SAS stores dates and times as single, unique numbers so that you can use them in programs like any other numeric variable:
A SAS date value is a value that represents the number of days between January 1, 1960, and a specified date. SAS can perform calculations on dates ranging from A.D. 1582 to A.D. 19,900. Dates before January 1, 1960, are negative numbers; dates after are positive numbers.
/*************************************/
/* set system options for report */
/*************************************/
options nodate nonumber;
options nodate pageno=1 linesize=64 pagesize=60;
/*************************************/
/* create temporary data set */
/*************************************/
data test;
Time1=86399;
format Time1 datetime.;
Date1=86399;
format Date1 date.;
Time2=86399;
format Time2 timeampm.;
Date1Month=month(Date1);
run;
/*************************************/
/* OUTPUT */
/*************************************/
The SAS System
Obs Time1 Date1 Time2 Date1Month
1 01JAN60:23:59:59 20JUL96 11:59:59 PM 7
SAS stores date value as numeric values while SAS date is stored as numbers.
SAS stores dates and times as single, unique numbers so that you can use them in programs like any other numeric variable:
A SAS date value is a value that represents the number of days between January 1, 1960, and a specified date. SAS can perform calculations on dates ranging from A.D. 1582 to A.D. 19,900. Dates before January 1, 1960, are negative numbers; dates after are positive numbers.
/*************************************/
/* set system options for report */
/*************************************/
options nodate nonumber;
options nodate pageno=1 linesize=64 pagesize=60;
/*************************************/
/* create temporary data set */
/*************************************/
data test;
Time1=86399;
format Time1 datetime.;
Date1=86399;
format Date1 date.;
Time2=86399;
format Time2 timeampm.;
Date1Month=month(Date1);
run;
/*************************************/
/* OUTPUT */
/*************************************/
The SAS System
Obs Time1 Date1 Time2 Date1Month
1 01JAN60:23:59:59 20JUL96 11:59:59 PM 7
Subset data
/*************************************/
/* select observations and variables */
/*************************************/
data subset1;
set admit;
if fee>= 124.80 & sex = 'M';
keep id name age weight;
run;
The KEEP statement specifies that only the variables ID, Name, Age, and Weight appear in the subset data. You can use the DROP statement instead if more variables are kept than dropped.
/*************************************/
/* subset data, perform processing, */
/* and subset variables again */
/*************************************/
data subset2(keep=id sex kgweight);
set admit(drop=name date);
if actlevel='LOW' and age>40;
KgWeight=weight/2.2;
run;
The DROP= data set option in the SET statement prevents the variables Name and Date from being read from the input data set (and therefore from appearing in the output data set).
/* select observations and variables */
/*************************************/
data subset1;
set admit;
if fee>= 124.80 & sex = 'M';
keep id name age weight;
run;
The KEEP statement specifies that only the variables ID, Name, Age, and Weight appear in the subset data. You can use the DROP statement instead if more variables are kept than dropped.
/*************************************/
/* subset data, perform processing, */
/* and subset variables again */
/*************************************/
data subset2(keep=id sex kgweight);
set admit(drop=name date);
if actlevel='LOW' and age>40;
KgWeight=weight/2.2;
run;
The DROP= data set option in the SET statement prevents the variables Name and Date from being read from the input data set (and therefore from appearing in the output data set).
Create and redefine variables
/*************************************/
/* create and redefine variables */
/* with assignment, function */
/*************************************/
data diabetes_female;
set diabetes;
if sex='F';
GlucoseChange=postglucose-fastglucose;
fastglucose=fastglucose+fastglucose*.10;
AvgGlucose=mean(postglucose,fastglucose);
run;
/*************************************/
/* create variables with LENGTH */
/*************************************/
data PatientAccounts;
set diabetes(keep=id sex age);
length Group $ 8;
if age>=55 then group='Seniors';
else group='Under 55';
run;
You can use the LENGTH statement to create a variable and set the length of the variable. Here the LENGTH statement assigns a length to accommodate the longest value of the variable Group. The longest value is Under 55, which has eight characters. Because Group is a character variable, you must follow the variable name with a dollar sign ($).
For character variables, you must allow for the longest possible value in the first statement that uses the variable, because you cannot change the length with a subsequent LENGTH statement within the same DATA step. The maximum length of any character variable in the SAS System is 32,767 bytes. For numeric variables, you can change the length of the variable by using a subsequent LENGTH statement.
/*************************************/
/* create variable with FORMAT */
/*************************************/
data sales;
format Sale_Price 6.2;
Sale_Price=49.99;run;
/*************************************/
/* create variables with ATTRIB */
/*************************************/
data sales;
attrib Sale_Price format=6.2
label="Sale Price";
Sale_Price=49.99;
run;
/* create and redefine variables */
/* with assignment, function */
/*************************************/
data diabetes_female;
set diabetes;
if sex='F';
GlucoseChange=postglucose-fastglucose;
fastglucose=fastglucose+fastglucose*.10;
AvgGlucose=mean(postglucose,fastglucose);
run;
/*************************************/
/* create variables with LENGTH */
/*************************************/
data PatientAccounts;
set diabetes(keep=id sex age);
length Group $ 8;
if age>=55 then group='Seniors';
else group='Under 55';
run;
You can use the LENGTH statement to create a variable and set the length of the variable. Here the LENGTH statement assigns a length to accommodate the longest value of the variable Group. The longest value is Under 55, which has eight characters. Because Group is a character variable, you must follow the variable name with a dollar sign ($).
For character variables, you must allow for the longest possible value in the first statement that uses the variable, because you cannot change the length with a subsequent LENGTH statement within the same DATA step. The maximum length of any character variable in the SAS System is 32,767 bytes. For numeric variables, you can change the length of the variable by using a subsequent LENGTH statement.
/*************************************/
/* create variable with FORMAT */
/*************************************/
data sales;
format Sale_Price 6.2;
Sale_Price=49.99;run;
/*************************************/
/* create variables with ATTRIB */
/*************************************/
data sales;
attrib Sale_Price format=6.2
label="Sale Price";
Sale_Price=49.99;
run;
Submit SAS programs to remote hosts (SAS/CONNECT)
options comamid=netbios remote=netpc;
The OPTIONS statement specifies the COMAMID= and the REMOTE= system options. These two system options define to the local session what type of link you want to establish to which remote host.
libname lhost 'c:\sales\reg1';
This LIBNAME statement defines a libref for the SAS library on the local session where the downloaded data set should be stored.
signon;
The SIGNON statement signs on to the remote host. You don't have to include a remote session ID when you have defined the REMOTE= system option in a previous OPTIONS statement.
rsubmit;
The RSUBMIT statement sends statements to the remote session for processing with the RSUBMIT statement. All statements are sent to the remote session until an ENDRSUBMIT statement is encountered.
Although you don't have to include a remote session ID, using a remote session ID in the RSUBMIT statement clarifies which remote session should process a group of statements when more than one link is active. If you omit the remote session ID, the RSUBMIT statement submits the statements to the remote session that is most recently identified in a SIGNON or an RSUBMIT statement or a REMOTE= system option.
libname rhost 'd:\dept12';
This LIBNAME statement defines the libref for the SAS library on the remote host.
proc download data=rhost.sales
out=lhost.sales;
run;
The PROC DOWNLOAD step transfers the data from the library on the remote host (RHOST) to the library on the local host (LHOST).
endrsubmit;
The ENDRSUBMIT statement signals the end of the block of statements that is to be submitted to the remote session. Statements following the ENDRSUBMIT statement are processed by the local session.
The OPTIONS statement specifies the COMAMID= and the REMOTE= system options. These two system options define to the local session what type of link you want to establish to which remote host.
libname lhost 'c:\sales\reg1';
This LIBNAME statement defines a libref for the SAS library on the local session where the downloaded data set should be stored.
signon;
The SIGNON statement signs on to the remote host. You don't have to include a remote session ID when you have defined the REMOTE= system option in a previous OPTIONS statement.
rsubmit;
The RSUBMIT statement sends statements to the remote session for processing with the RSUBMIT statement. All statements are sent to the remote session until an ENDRSUBMIT statement is encountered.
Although you don't have to include a remote session ID, using a remote session ID in the RSUBMIT statement clarifies which remote session should process a group of statements when more than one link is active. If you omit the remote session ID, the RSUBMIT statement submits the statements to the remote session that is most recently identified in a SIGNON or an RSUBMIT statement or a REMOTE= system option.
libname rhost 'd:\dept12';
This LIBNAME statement defines the libref for the SAS library on the remote host.
proc download data=rhost.sales
out=lhost.sales;
run;
The PROC DOWNLOAD step transfers the data from the library on the remote host (RHOST) to the library on the local host (LHOST).
endrsubmit;
The ENDRSUBMIT statement signals the end of the block of statements that is to be submitted to the remote session. Statements following the ENDRSUBMIT statement are processed by the local session.
Read PC database files
To read PC database files, you use the IMPORT procedure. PROC IMPORT reads the input file and writes the data to a SAS data set, with the SAS variables defined based on the input records.
/*************************************/
/* import the Excel file */
/*************************************/
proc import datafile="c:\myfiles\Accounts.xls"
out=sasuser.accounts;
sheet="Prices";
getnames=no;
run;
/*************************************/
/* print part of the new data set */
/*************************************/
proc print data=sasuser.accounts(obs=10);
run;
/*************************************/
/* import the Access file */
/*************************************/
proc import table="customers"
out=sasuser.cust dbms=access;
uid="userid";
pwd="mypassword";
database="c:\myfiles\east.mdb";
wgdb="c:\winnt\system32\security.mdb";
run;
/*************************************/
/* print part of the new data set */
/*************************************/
proc print data=sasuser.cust(obs=5);
run;
/*************************************/
/* create variables with INPUT */
/*************************************/
data diabetes;
input ID $ Sex $ Age Height Weight
Pulse FastGlucose PostGlucose;
datalines;
2304 F 16 61 102 100 568 625
1128 M 43 71 218 76 156 208
4425 F 48 66 162 80 244 322
1387 F 57 64 142 70 177 206
9012 F 39 63 157 68 257 318
6312 M 52 72 240 77 362 413
;
run;
/*You can use keyword datalines; or cards; */
/*************************************/
/* import the Excel file */
/*************************************/
proc import datafile="c:\myfiles\Accounts.xls"
out=sasuser.accounts;
sheet="Prices";
getnames=no;
run;
/*************************************/
/* print part of the new data set */
/*************************************/
proc print data=sasuser.accounts(obs=10);
run;
/*************************************/
/* import the Access file */
/*************************************/
proc import table="customers"
out=sasuser.cust dbms=access;
uid="userid";
pwd="mypassword";
database="c:\myfiles\east.mdb";
wgdb="c:\winnt\system32\security.mdb";
run;
/*************************************/
/* print part of the new data set */
/*************************************/
proc print data=sasuser.cust(obs=5);
run;
/*************************************/
/* create variables with INPUT */
/*************************************/
data diabetes;
input ID $ Sex $ Age Height Weight
Pulse FastGlucose PostGlucose;
datalines;
2304 F 16 61 102 100 568 625
1128 M 43 71 218 76 156 208
4425 F 48 66 162 80 244 322
1387 F 57 64 142 70 177 206
9012 F 39 63 157 68 257 318
6312 M 52 72 240 77 362 413
;
run;
/*You can use keyword datalines; or cards; */
Read SAS data sets
data canada;
set mylib.productsales;
if country='CANADA';
run;
In this DATA step, each observation in the data set Mylib.ProductSales is read into the program data vector. Only those observations whose value of COUNTRY is CANADA are output to the new data set Canada.
data canada2;
set mylib.productsales;
if country='CANADA';
Total_Variance=actual-predict;
Forecast=actual*1.15;
run;
This DATA step creates data set Canada2 by reading data from data set Mylib.ProductSales and by calculating values for the two new variables Total_Variance and Forecast.
data product_sample;
do obsnum=1 to 100 by 2;
set mylib.productsales point=obsnum;
if _error_ then abort;
output;
end;
stop;
run;
These statements create the data set Product_Sample by selecting a subset of 50 observations from the data set Mylib.ProductSales, using the POINT= option to access observations directly by number.
set mylib.productsales;
if country='CANADA';
run;
In this DATA step, each observation in the data set Mylib.ProductSales is read into the program data vector. Only those observations whose value of COUNTRY is CANADA are output to the new data set Canada.
data canada2;
set mylib.productsales;
if country='CANADA';
Total_Variance=actual-predict;
Forecast=actual*1.15;
run;
This DATA step creates data set Canada2 by reading data from data set Mylib.ProductSales and by calculating values for the two new variables Total_Variance and Forecast.
data product_sample;
do obsnum=1 to 100 by 2;
set mylib.productsales point=obsnum;
if _error_ then abort;
output;
end;
stop;
run;
These statements create the data set Product_Sample by selecting a subset of 50 observations from the data set Mylib.ProductSales, using the POINT= option to access observations directly by number.
Subscribe to:
Posts (Atom)