Let us start with inserting data in sas dataset
- How
will the output data look like -
Title 'Survey';
DATA
survey;
INPUT id sex age inc r1 r2 r3 ;
DATALINES;
1 F 35 17 7 2 2
17 M 50 14 5 5 3
33 F 45 6 7 2 7
49 M 24 14 7 5 7
INPUT id sex age inc r1 r2 r3 ;
DATALINES;
1 F 35 17 7 2 2
17 M 50 14 5 5 3
33 F 45 6 7 2 7
49 M 24 14 7 5 7
;
RUN;
After running above code in SAS output will be
Survey
| Obs | id | sex | age | inc | r1 | r2 | r3 |
|---|---|---|---|---|---|---|---|
| 1 | 1 | . | 35 | 17 | 7 | 2 | 2 |
| 2 | 17 | . | 50 | 14 | 5 | 5 | 3 |
| 3 | 33 | . | 45 | 6 | 7 | 2 | 7 |
| 4 | 49 | . | 24 | 14 | 7 | 5 | 7 |
As you can Sex column is having a missing value though it is present in our insert statement.This had happened because SAS is considering it to be numeric value since $ sign is not present in front of variable name.
So we need to modify our insert statement to take character value by adding $ sign in front of the variable name like 'sex $'.
Title 'Survey';
DATA survey;
INPUT id sex $ age inc r1 r2 r3 ;
DATALINES;
1 F 35 17 7 2 2
17 M 50 14 5 5 3
33 F 45 6 7 2 7
49 M 24 14 7 5 7
INPUT id sex $ age inc r1 r2 r3 ;
DATALINES;
1 F 35 17 7 2 2
17 M 50 14 5 5 3
33 F 45 6 7 2 7
49 M 24 14 7 5 7
;
RUN;
Output
Survey
| Obs | id | sex | age | inc | r1 | r2 | r3 |
|---|---|---|---|---|---|---|---|
| 1 | 1 | F | 35 | 17 | 7 | 2 | 2 |
| 2 | 17 | M | 50 | 14 | 5 | 5 | 3 |
| 3 | 33 | F | 45 | 6 | 7 | 2 | 7 |
| 4 | 49 | M | 24 | 14 | 7 | 5 | 7 |
Q2. Data cats;
infile '/folders/myfolders/Cat.txt';
Input ID $ 1-4 Age 6-7 Sex $ 8-9;
run;
Write a program, similar to the above
example that will create a temporary dataset called Students. Assume that
i)
temporary dataset (called cats,
work.cats, or 'cats.sas7bdat'(in some folder) already exists.
ii)
using the raw data file
directly.
What changes should be made so that the
DATA step reads only the first 15 observations?
Ans:
Lets create cat text file with 20 observations
i) data Students;
set cat;
run;
ii)Using raw file as input, the code require to read only 15 observations(rows) from file is as follows
Ans:
Lets create cat text file with 20 observations
i) data Students;
set cat;
run;
ii)Using raw file as input, the code require to read only 15 observations(rows) from file is as follows
data cat;
infile '/folders/myfolders/cat.txt' firstobs=1 obs=15;
input ID $ 1-4 AGE 6-7 SEX $ 8-9;
run;
proc print data=cat;
run;
input ID $ 1-4 AGE 6-7 SEX $ 8-9;
run;
proc print data=cat;
run;
In the above the option firstobs and obs are used to read first 15 rows from file.
Output:
Students
| Obs | ID | AGE | SEX |
|---|---|---|---|
| 1 | 0001 | 21 | M |
| 2 | 0002 | 21 | M |
| 3 | 0003 | 21 | M |
| 4 | 0004 | 21 | M |
| 5 | 0005 | 21 | M |
| 6 | 0006 | 21 | M |
| 7 | 0007 | 21 | M |
| 8 | 0008 | 21 | M |
| 9 | 0009 | 21 | M |
| 10 | 0010 | 21 | M |
| 11 | 0011 | 21 | M |
| 12 | 0012 | 21 | M |
| 13 | 0013 | 21 | M |
| 14 | 0014 | 21 | M |
| 15 | 0015 | 21 | M |
Q 3. Copy the below data in a .txt and then import into SAS
Joseph,76,"Red
Racers, Washington",AAA
Mitchel,82,"Blue
Bunnies, Richmond",AAA
Sue Ellen,74,"Green Gazelles, Atlanta",AA
Sue Ellen,74,"Green Gazelles, Atlanta",AA
Ans:
i) with Infile option:
Data Team;
infile '/folders/myfolders/Team.txt' dlm=',' dsd ;
Input Name: $20. Age Address: $50. Remarks: $10.;
run;
proc print data=Team; run;
Q 4. The following data gives the high and low trading prices in Canadian dollars for various stocks on a given day on the Toronto Stock Exchange. Enter these data into a SAS data set with three variables, Stock, Hi, and Low and 10 observations. Print the data set in the Output window to check that you have successfully entered it. Save the data set as a permanent SAS data set giving it the name stocks.
i) with Infile option:
Data Team;
infile '/folders/myfolders/Team.txt' dlm=',' dsd ;
Input Name: $20. Age Address: $50. Remarks: $10.;
run;
proc print data=Team; run;
In the code we have used dsd (delimeter sensitive data) in order to make sas understand not to consider comma of 'address' variable as delimeter.
ii)Proc Import
Title'Team';
Proc import datafile='/folders/myfolders/Team.txt' out=TeamImport dbms=dlm replace;
delimiter=',';
getnames=yes;
run;
proc print data=TeamImport; run;
Proc import datafile='/folders/myfolders/Team.txt' out=TeamImport dbms=dlm replace;
delimiter=',';
getnames=yes;
run;
proc print data=TeamImport; run;
output:
Team
| Obs | Name | Age | Address | Remarks |
|---|---|---|---|---|
| 1 | Joseph | 76 | Red Racers, Washington | AAA |
| 2 | Mitchel | 82 | Blue Bunnies, Richmond | AAA |
| 3 | Sue Ellen | 74 | Green Gazelles, Atlanta | AA |
Q 4. The following data gives the high and low trading prices in Canadian dollars for various stocks on a given day on the Toronto Stock Exchange. Enter these data into a SAS data set with three variables, Stock, Hi, and Low and 10 observations. Print the data set in the Output window to check that you have successfully entered it. Save the data set as a permanent SAS data set giving it the name stocks.
Stock Hi Low
ACR 7.95 7.80
MGI 4.75 4.00
BLD 112.25 109.75
CFP 9.65 9.25
MAL 8.25 8.10
CM 45.90 45.30
AZC 1.99 1.93
CMW 20.00 19.00
AMZ 2.70 2.30
GAC 52.00 50.25
Ans:
libname assign '/folders/myfolders/';
Data assign.Stocks;
input Stock $ Hi Low;
datalines;
ACR 7.95 7.80
MGI 4.75 4.00
BLD 112.25 109.75
CFP 9.65 9.25
MAL 8.25 8.10
CM 45.90 45.30
AZC 1.99 1.93
CMW 20.00 19.00
AMZ 2.70 2.30
GAC 52.00 50.25
;
run;
libname assign '/folders/myfolders/';
Data assign.Stocks;
input Stock $ Hi Low;
datalines;
ACR 7.95 7.80
MGI 4.75 4.00
BLD 112.25 109.75
CFP 9.65 9.25
MAL 8.25 8.10
CM 45.90 45.30
AZC 1.99 1.93
CMW 20.00 19.00
AMZ 2.70 2.30
GAC 52.00 50.25
;
run;
Proc Print data=assign.Stocks;
run;
Output:
getnames=yes;
run;
Output:
data assign.Stocks;
set assign.stocks;
Average=mean(hi,low);
run;
proc print data=assign.Stocks;
run;
Ans: In this we will be first creating the temp dataset and then merge it to the existing dataset.
Data StocksTemp;
input Stock $ Hi Low;
cards;
CLV 1.85 1.78
SIL 34.00 34.00
AC 14.45 14.05
;
run;
Data Assign.stocks;
Set assign.stocks(drop=Average) Stockstemp;
run;
Proc Print data=assign.stocks;run;
Output:
Stocks
Q 8. Save the following data in a .txt file and import into SAS.
Title'Personal Info';
Proc import datafile='/folders/myfolders/UPLDwithImport.txt' out=PersonalInfo dbms=dlm replace;
delimiter=',';
getnames=yes;
run;
proc print data=personalinfo; run;
Output:
Personal Info
run;
Output:
Stocks
| Obs | Stock | Hi | Low |
|---|---|---|---|
| 1 | ACR | 7.95 | 7.80 |
| 2 | MGI | 4.75 | 4.00 |
| 3 | BLD | 112.25 | 109.75 |
| 4 | CFP | 9.65 | 9.25 |
| 5 | MAL | 8.25 | 8.10 |
| 6 | CM | 45.90 | 45.30 |
| 7 | AZC | 1.99 | 1.93 |
| 8 | CMW | 20.00 | 19.00 |
| 9 | AMZ | 2.70 | 2.30 |
| 10 | GAC | 52.00 | 50.25 |
5. Save
the above data in a text file (.txt) and import the file into SAS.
Ans:
Proc import datafile='/folders/myfolders/StocksImport.txt' out=Assign.StockImport dbms=dlm replace;getnames=yes;
run;
Output:
Stock Import
| Obs | Stock | Hi | Low |
|---|---|---|---|
| 1 | ACR | 7.95 | 7.8 |
| 2 | MGI | 4.75 | 4 |
| 3 | BLD | 112.25 | 109.75 |
| 4 | CFP | 9.65 | 9.25 |
| 5 | MAL | 8.25 | 8.1 |
| 6 | CM | 45.9 | 45.3 |
| 7 | AZC | 1.99 | 1.93 |
| 8 | CMW | 20 | 19 |
| 9 | AMZ | 2.7 | 2.3 |
| 10 | GAC | 52 | 50.25 |
6. Calculate
the average of the Hi and Low prices for all the stocks and save it in a
variable called
average.
Ans:
set assign.stocks;
Average=mean(hi,low);
run;
proc print data=assign.Stocks;
run;
Output: Stocks
| Obs | Stock | Hi | Low | Average |
|---|---|---|---|---|
| 1 | ACR | 7.95 | 7.80 | 7.875 |
| 2 | MGI | 4.75 | 4.00 | 4.375 |
| 3 | BLD | 112.25 | 109.75 | 111.000 |
| 4 | CFP | 9.65 | 9.25 | 9.450 |
| 5 | MAL | 8.25 | 8.10 | 8.175 |
| 6 | CM | 45.90 | 45.30 | 45.600 |
| 7 | AZC | 1.99 | 1.93 | 1.960 |
| 8 | CMW | 20.00 | 19.00 | 19.500 |
| 9 | AMZ | 2.70 | 2.30 | 2.500 |
| 10 | GAC | 52.00 | 50.25 | 51.125 |
7. Using
the SAS data set created in Exercise 4, add the following stocks to the
permanent data set.
Stock Hi Low
CLV 1.85 1.78
SIL 34.00 34.00
AC 14.45 14.05
Remove the variable average from the data
set and save the data set.
Ans: In this we will be first creating the temp dataset and then merge it to the existing dataset.
Data StocksTemp;
input Stock $ Hi Low;
cards;
CLV 1.85 1.78
SIL 34.00 34.00
AC 14.45 14.05
;
run;
Data Assign.stocks;
Set assign.stocks(drop=Average) Stockstemp;
run;
Proc Print data=assign.stocks;run;
Output:
Stocks
| Obs | Stock | Hi | Low |
|---|---|---|---|
| 1 | ACR | 7.95 | 7.80 |
| 2 | MGI | 4.75 | 4.00 |
| 3 | BLD | 112.25 | 109.75 |
| 4 | CFP | 9.65 | 9.25 |
| 5 | MAL | 8.25 | 8.10 |
| 6 | CM | 45.90 | 45.30 |
| 7 | AZC | 1.99 | 1.93 |
| 8 | CMW | 20.00 | 19.00 |
| 9 | AMZ | 2.70 | 2.30 |
| 10 | GAC | 52.00 | 50.25 |
| 11 | CLV | 1.85 | 1.78 |
| 12 | SIL | 34.00 | 34.00 |
| 13 | AC | 14.45 | 14.05 |
Note: Rows hilighted in red are newly added rows
Q 8. Save the following data in a .txt file and import into SAS.
Name, Score, Team,
Division
(data generated
from the recent Athletics Meet)
Joseph,76,"Red
Racers, Washington",AAA
Mitchel,82,"Blue
Bunnies, Richmond",AAA
Sue
Ellen,74,"Green Gazelles, Atlanta",AA
Ans:Title'Personal Info';
Proc import datafile='/folders/myfolders/UPLDwithImport.txt' out=PersonalInfo dbms=dlm replace;
delimiter=',';
getnames=yes;
run;
proc print data=personalinfo; run;
Output:
Personal Info
| Obs | Name | _Score | _Team | _Division |
|---|---|---|---|---|
| 1 | Joseph | 76 | Red Racers, Washington | AAA |
| 2 | Mitchel | 82 | Blue Bunnies, Richmond | AAA |
| 3 | Sue Ellen | 74 | Green Gazelles, Atlanta | AA |
