Вы находитесь на странице: 1из 11

1) What will be the output if the following program is submitted?

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?

Creating Several Observations from OneLine of Data:


DATA DOUBLE;
INPUT X Y @@;
DATALINES;
123456
78
;
PROC PRINT DATA=DOUBLE;

TITLE 'DOUBLE TRAILING @';


RUN;
5) What will be the output of the following Program?
DATA TRAILING;
INPUT @6 TYPE $1. @;
IF TYPE = '1' THEN INPUT AGE 1-2;
ELSE IF TYPE = '2' THEN
INPUT AGE 3-4;
DROP TYPE;
DATALINES;
23 1
44 2
;
PROC PRINT DATA=TRAILING;
TITLE 'SINGLE TRAILING @';
RUN;
6) data weather;
infile datalines missover;
input temp1-temp5;
datalines;
97.9 98.1 98.398.6 99.2 99.1 98.5 97.5
96.2 97.3 98.3 97.6 96.5
;
Run;
Proc print data=weather;
run;
7) data mydata;
Input id x1 x2 x3 x4 x5;
datalines;
12....
22121.
3.1.1.
41...1
5...1.
;
data mydata2;
set mydata;
array test(5) x1-x5;
array hold(5);

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;

proc print data = temp3 noobs;


format date new_month date9.;
run;
id date new_month
1 12NOV1980 01DEC1980
2 20OCT1996 01NOV1996
3 21DEC1999 01JAN2000
Now let's try another example, this time creating a variable that is two days later than
the day given in our data set.
data temp3a;
set temp2;
two_days = intnx('day',date,2);
run;
proc print data = temp3a noobs;
format date two_days date9.;
run;
id date two_days
1 12NOV1980 14NOV1980
2 20OCT1996 22OCT1996
3 21DEC1999 23DEC1999
10) Reading an Excel file into SAS
Suppose that you have an Excel spreadsheet called auto.xls. The data for this
spreadsheet are shown below.
MAKE MPG WEIGHT PRICE
AMC Concord 22 2930 4099
AMC Pacer 17 3350 4749
AMC Spirit 22 2640 3799
Buick Century 20 3250 4816
Buick Electra 15 4080 7827
Using the Import Wizard is an easy way to import data into SAS. The Import Wizard can
be found on the drop down file menu. Although the Import Wizard is easy it can be
time consuming if used repeatedly. The very last screen of the Import Wizard gives you
the option to save the statements SAS uses to import the data so that it can be used
again. The following is an example that uses common options and also shows that the
file was imported correctly.
PROC IMPORT OUT= WORK.auto1

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

your variables (e.g., a tab separated file).


DSD The dsd option has 2 functions. First, it recognizes two consecutive delimiters as
a missing value. For example, if your file contained the line 20,30,,50 SAS will treat
this as 20 30 50 but with the the dsd option SAS will treat it as 20 30 . 50 , which is
probably what you intended. Second, it allows you to include the delimiter within
quoted strongs. For example, you would want to use the dsd option if you had a
comma separated file and your data included values like "George Bush, Jr.". With the
dsd option, SAS will recognize that the comma in "George Bush, Jr." is part of the
name, and not a separator indicating a new variable.
FIRSTOBS=This option tells SAS what on what line you want it to start reading your raw
data file. If the first record(s) contains header information such as variable names,
then set firstobs=n where n is the record number where the data actually begin. For
example, if you are reading a comma separated file or a tab separated file that has
the variable names on the first line, then use firstobs=2 to tell SAS to begin reading at
the second line (so it will ignore the first line with the names of the variables).
MISSOVER This option prevents SAS from going to a new input line if it does not find
values for all of the variables in the current line of data. For example, you may be
reading a space delimited file and that is supposed to have 10 values per line, but one
of the line had only 9 values. Without the missover option, SAS will look for the 10th
value on the next line of data. If your data is supposed to only have one observation
for each line of raw data, then this could cause errors throughout the rest of your data
file. If you have a raw data file that has one record per line, this option is a prudent
method of trying to keep such errors from cascading through the rest of your data file.
OBS= Indicates which line in your raw data file should be treated as the last record to
be read by SAS. This is a good option to use for testing your program. For example, you
might use obs=100 to just read in the first 100 lines of data while you are testing your
program. When you want to read the entire file, you can remove the obs= option
entirely.
A typical infile statement for reading a comma delimited file that contains the
variable names in the first line of data would be:
INFILE "test.txt" DLM=',' DSD MISSOVER FIRSTOBS=2 ;
12)How do I read a delimited file that has embedded delimiters in the data?
Suppose you are reading a comma separated file, but your data contains commas in it.
For example, say your file contains age name and weight and looks like the one below.
48,'Bill Clinton',210

50,'George Bush, Jr.',180


Say you read this file as you would any other comma delimited file, like the example
shown below.
DATA guys1;
length name $ 20 ;
INFILE 'readdsd2.txt' DELIMITER=',' ;
INPUT age name weight ;
RUN;
PROC PRINT DATA=guys1;
RUN;
But, as we see below, the data were not read as we wished. The quotes are treated as
data, and George Bush lost the , Jr off his name, and his weight is missing. This is
because SAS treated the , in George Bush's name as a indicating the end of the
variable, which is not what we wanted.OBS NAME AGE WEIGHT
1 'Bill Clinton' 48 210
2 'George Bush 50 .
Below, we use the dsd option to read the same file.
DATA guys2;
length name $ 20 ;
INFILE 'readdsd2.txt' DELIMITER=',' DSD ;
INPUT age name weight ;
RUN;
PROC PRINT DATA=guys2;
RUN;
As you see in the output below, SAS properly treated the quotes as delimiters, and it
read in Mr. Bush's name properly and his weight properly. OBS NAME AGE WEIGHT
1 Bill Clinton 48 210
2 George Bush, Jr. 50 180
13)

Вам также может понравиться