Академический Документы
Профессиональный Документы
Культура Документы
Data one;
input lastname: $15. typeofcar: $15. mileage;
datalines;
Jones Toyota 7435
Smith Toyota 13001
Jones2 Ford 3433
Smith2 Toyota 15032
Shepherd Nissan 4300
Shepherd2 Honda 5582
Williams Ford 10532
;
Data two;
input startrange endrange typeofservice & $35.;
datalines;
3000 5000 oil change
5001 6000 overdue oil change
6001 8000 oil change and tire rotation
8001 9000 overdue oil change
9001 11000 oil change
11001 12000 overdue oil change
12001 14000 oil change and tire rotation
14001 14999 overdue oil change
15000 15999 15000 mile check
;
data combine;
set one;
found=0;
do i=1 to nobs until (found);
set two point=i nobs=nobs;
if startrange <= mileage <= endrange then do;
output;
found=1;
end;
end;
run;
proc print;
run;
2) What is the output if I submitted following program?
data one; drop i;
input v1-v4; array v(4);
count=0;
do i=1 to 4 while (v{i}=1);
count +1;
end;
datalines;
1101
1110
1011
0100
;
proc print;
run;
3) What will be the output if I submitted following Programs?
data test;
input var1 var2 var3;
datalines;
10 20 30
100 . 300
. 40 400
;
data new (drop=i);
set test;
array newval(3)_TEMPORARY_ (.1 .2 .3) ;
array now(3) var1 var2 var3;
do i=1 to 3;
if now(i)=. then now(i)=newval(i);
end;run;
proc print;
run;
4) What will be the output of the following Program if I submitted?
count=0;
do i=1 to 5;
if test(i) =. then count+1;
if test(i) ne . or i=5 then do;
hold(i)=count;
count=0;
end;
end;
highcnt=max(of hold1-hold5);
drop i count;
run;
proc print noobs;
run;
8) DATA auto ;
INPUT make $ mpg rep78 weight foreign ;
CARDS;
AMC 22 3 2930 0
AMC 17 3 3350 0
AMC 22 . 2640 0
Audi 17 5 2830 1
Audi 23 3 2070 1
BMW 25 4 2650 1
Buick 20 3 3250 0
Buick 15 4 4080 0
Buick 18 3 3670 0
Buick 26 . 2230 0
Buick 20 3 3280 0
Buick 16 3 3880 0
Buick 19 3 3400 0
Cad. 14 3 4330 0
Cad. 14 2 3900 0
Cad. 21 3 4290 0
Chev. 29 3 2110 0
Chev. 16 4 3690 0
Chev. 22 3 3180 0
Chev. 22 2 3220 0
Chev. 24 2 2750 0
Chev. 19 3 3430 0
Datsun 23 4 2370 1
Datsun 35 5 2020 1
Datsun 24 4 2280 1
Datsun 21 4 2750 1
;
RUN;
DATA auto2;
SET auto;
LABEL rep78 ="1978 Repair Record"
mpg ="Miles Per Gallon"
foreign="Where Car Was Made";
RUN;
PROC CONTENTS DATA=auto2;
RUN;
9) The intnx function increments dates by intervals. It computes the date (or datetime)
of the start of each interval. For example, let's suppose that you had a column of days
of the month, and you wanted to create a new variable that was the first of the next
month. You could use the intnx function to help you create your new variable.
The syntax of the intnx function is: intnx(interval, from, n <, alignment>), where
interval is a character (e.g., string) constant or variable, from is the starting value
(either a date or datetime), n is the number of intervals to increment, and alignment
is optional and controls the alignment of the dates.
data temp2;
input id 1 @3 date mmddyy11.;
cards;
1 11/12/1980
2 10/20/1996
3 12/21/1999
;
run;
proc print data = temp2;
format date date9.;
run;
id date
1 12NOV1980
2 20OCT1996
3 21DEC1999
data temp3;
set temp2;
new_month = intnx('month',date,1);
run;
DATAFILE= "C:\auto.xls"
DBMS=EXCEL REPLACE;
SHEET="auto1";
GETNAMES=YES;
MIXED=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
proc print data=auto1;
run;
Obs MAKE MPG WEIGHT PRICE
1 AMC Concord 22 2930 4099
2 AMC Pacer 17 3350 4749
3 Amc Spirit 22 2640 3799
4 Buick Century 20 3250 4816
5 Buick Electra 15 4080 7827
First we use the out= statement to tell SAS where to store the date once its imported.
Next the datafile= statement tells SAS where to find the file we want to import.
The dbms= statement is used to identify the type of file being imported.
This statement is redundant if the file you want to import already has an appropriate
file extension, for example *.xls.
The replace statement will overwrite an existing file.
To specify which sheet SAS should import use the sheet="sheetname" statement. The
default is for SAS to read the first sheet. Note that sheet names can only be 31
characters long.
The getnames=yes is the default setting and SAS will automatically use the first row of
data as variable names. If the first row of your sheet does not contain variable names
use the getnames=no.
SAS uses the first eight rows of data to determine whether the variable should be read
as character or numeric. The default setting mixed=no assumes that each variable is
either all character or all numeric. If you have a variable with both character and
numeric values or a variable with missing values use mixed=yes statement to be sure
SAS will read it correctly.
Conveniently SAS reads date, time and datetime formats. The usedate=yes is the
default statement and SAS will read date or time formatted data as a date. When
usedate=no SAS will read date and time formatted data with a datetime format. Keep
the default statement scantime=yes to read in time formatted data as long as the
variable does not also contain a date format.
Example 1: Making a permanent data file
What if you want the SAS data set created from proc import to be permanent? The
answer is to use libname statement. Let's say that we have an Excel file called auto.xls
in directory "d:\temp" and we want to convert it into a SAS data file (call it myauto)
and put it into the directory "c:\dissertation". Here is what we can do.
libname dis "c:\dissertation";
proc import datafile="d:\temp\auto.xls" out=dis.myauto replace;
run;
Example 2: Reading in a specific sheet
Sometimes you may only want to read a particular sheet from an Excel file instead of
the entire Excel file. Let's say that we have a two-sheet Excel file called auto2.xls.
The example below shows how to use the option sheet=sheetname to read the second
sheet called page2 in it.
proc import datafile="auto2.xls" out=auto1 replace;
sheet="page2";
run;
Example 3: Reading a file without variable names
What if the variables in your Excel file do not have variable names? The answer here is
to use the statement getnames=no in proc import. Here is an example showing how to
do this.
proc import datafile="a:\faq\auto.xls" out=auto replace;
getnames=no;
run;
Writing Excel files out from SAS
It is very easy to write out an Excel file using proc export in SAS version 8. Consider
the following sample data file below.
Obs MAKE MPG WEIGHT PRICE
1 AMC 22 2930 4099
2 AMC 17 3350 4749
3 AMC 22 2640 3799
4 Buick 20 3250 4816
5 Buick 15 4080 7827
Here is a sample program that writes out an Excel file called mydata.xls into the
directory "c:\dissertation".
proc export data=mydata outfile='c:\dissertation\mydata.xls' replace;
run;
11) What are some common options for the infile statement in SAS?
There are a large number of options that you can use on the infile statement. This is a
brief summary of commonly used options. You can determine which options you may
need by examining your raw data file e.g., in Notepad, Wordpad, using more (on UNIX)
or any other command that allows you to view your data.
Let's start with a simple example reading the space delimited file shown below.
22
17
22
20
15
2930
3350
2640
3250
4080
4099
4749
3799
4816
7827
The example program shows how to read the space delimited file shown above.
DATA cars;
INFILE 'space1.txt' ;
INPUT mpg weight price;
RUN;
PROC PRINT DATA=cars;
RUN;
As you can see in the output below, the data was read properly. OBS MPG WEIGHT
PRICE
1
2
3
4
5
22
17
22
20
15
2930
3350
2640
3250
4080
4099
4749
3799
4816
7827
Infile options
For more complicated file layouts, refer to the infile options described below.
DLM=The dlm= option can be used to specify the delimiter that separates the variables
in your raw data file. For example, dlm=','indicates a comma is the delimiter (e.g., a
comma separated file, .csv file). Or, dlm='09'x indicates that tabs are used to separate