BookmarkSubscribeRSS Feed
kumaabi
Fluorite | Level 6

Write a SAS program to do the following in order of the question numbers:

  1.   a. Create a SAS library, E11 that connects to the folder U:\STA575\Exam1. You must first create a folder Exam1 inside STA575 on your U drive and place fish.sas7bdat inside the folder.
  2.  b. Include a global options statement, where firstobs = 21 and obs = 35.
  3.    c.Write a data step that reads observations from the SAS data set fish and creates a temporary SAS data set one. Include an option keep= Species Weight Height in fish.sas7bdat data set.
  4.   d. By using appropriate procedure on the fish.sas7bdat data set, answer the following questions:
  • Number of variables and the number of observations in the data set,
  • the date the data was created,
  • the size of the data set in kilo bytes, and
  • the number of character variables.
  • e. Using local options, print the first five observations in one.sas7bdat data set in question c.
  • f. Print the data set work.one. How many observations are printed? How many observations are in work.one? Explain the reason behind the number of printed observations.

2.Write a SAS program to do the following in order of the question numbers:   

a. Reset the global options in question (1b) to their default values.

b. Write a data step that reads observations from the SAS data set fish and creates two temporary SAS data sets Type1 and Type2. Include the following ‘if’ statements: If Species = ‘Bream’ then output Type1; If Species=’Roach’ then output Type2;

c.   Using XLSX engine, write a libname statement that creates (or links) a new Excel file Fish.xlsx workbook in the folder Exam1 created in question (1a). Use the library reference E12.

 d. Using worksheet name Type1, write a data step that reads the Type1 SAS data set in (b) into the Excel workbook Fish.xlsx in 2(c).

e.Using worksheet name Type2, write a data step that reads the Type2 SAS data set in (b) into the Excel workbook Fish.xlsx in 2(c).

 f.How many observations are in Type1 and Type 2 data sets in (b)?

7 REPLIES 7
kumaabi
Fluorite | Level 6

Write a SAS program to do the following in order of the question numbers:

  1.   a. Create a SAS library, E11 that connects to the folder U:\STA575\Exam1. You must first create a folder Exam1 inside STA575 on your U drive and place fish.sas7bdat inside the folder.
  2.  b. Include a global options statement, where firstobs = 21 and obs = 35.
  3.    c.Write a data step that reads observations from the SAS data set fish and creates a temporary SAS data set one. Include an option keep= Species Weight Height in fish.sas7bdat data set.
  4.   d. By using appropriate procedure on the fish.sas7bdat data set, answer the following questions:
  • Number of variables and the number of observations in the data set,
  • the date the data was created,
  • the size of the data set in kilo bytes, and
  • the number of character variables.
  • e. Using local options, print the first five observations in one.sas7bdat data set in question c.
  • f. Print the data set work.one. How many observations are printed? How many observations are in work.one? Explain the reason behind the number of printed observations.

2.Write a SAS program to do the following in order of the question numbers:   

a. Reset the global options in question (1b) to their default values.

b. Write a data step that reads observations from the SAS data set fish and creates two temporary SAS data sets Type1 and Type2. Include the following ‘if’ statements: If Species = ‘Bream’ then output Type1; If Species=’Roach’ then output Type2;

c.   Using XLSX engine, write a libname statement that creates (or links) a new Excel file Fish.xlsx workbook in the folder Exam1 created in question (1a). Use the library reference E12.

 d. Using worksheet name Type1, write a data step that reads the Type1 SAS data set in (b) into the Excel workbook Fish.xlsx in 2(c).

e.Using worksheet name Type2, write a data step that reads the Type2 SAS data set in (b) into the Excel workbook Fish.xlsx in 2(c).

 f.How many observations are in Type1 and Type 2 data sets in (b)?

mkeintz
PROC Star

I don't think you are likely to get answers to your homework on this forum.  However, you might get some helpful hints if you report what you have tried so far.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

Let us see what you have already done so far. Post your code using the "little running man" button.

Then tell us where you are stuck, so we can give you advice.

kumaabi
Fluorite | Level 6
libname E11 'U:\STA575\Exam1';
data one;
set E11.fish;
options firstobs = 21 obs = 35;
keep Species Weight Height;
run;
proc contents data = one;
run;

proc print data = one(firstobs = 1 obs = 5);
run;
proc print data = one;
run;
goptions reset;
data type1 type2;
set E11.fish;
if (Species ='Bream') then output type1;
if (Species='Roach') then output type2;
run;
libname E12 xlsx'U:\STA575\Exam1\Fish.xlsx';
data E12.Type1;
set type1;
run;
data E12.Type2;
set type2;
run;



Getting error for question 2d, 2e, 2f
kumaabi
Fluorite | Level 6

1. Write a SAS program to do the following in order of the question numbers:
a. Create a SAS library, E11 that connects to the folder U:\STA575\Exam1. You must first create a folder Exam1 inside STA575 on your U drive and place fish.sas7bdat inside the folder.
b. Include a global options statement, where firstobs = 21 and obs = 35.
c. Write a data step that reads observations from the SAS data set fish and creates a temporary SAS data set one. Include an option keep= Species Weight Height in fish.sas7bdat data set.
d. By using appropriate procedure on the fish.sas7bdat data set, answer the following questions:
• Number of variables and the number of observations in the data set,
• the date the data was created,
• the size of the data set in kilo bytes, and
• the number of character variables.
e. Using local options, print the first five observations in one.sas7bdat data set in question c.
f. Print the data set work.one. How many observations are printed? How many observations are in work.one? Explain the reason behind the number of printed observations.

2. Write a SAS program to do the following in order of the question numbers:
a. Reset the global options in question (1b) to their default values.
b. Write a data step that reads observations from the SAS data set fish and creates two temporary SAS data sets Type1 and Type2. Include the following ‘if’ statements: If Species = ‘Bream’ then output Type1; If Species=’Roach’ then output Type2;
c. Using XLSX engine, write a libname statement that creates (or links) a new Excel file Fish.xlsx workbook in the folder Exam1 created in question (1a). Use the library reference E12.
d. Using worksheet name Type1, write a data step that reads the Type1 SAS data set in (b) into the Excel workbook Fish.xlsx in 2(c).
e. Using worksheet name Type2, write a data step that reads the Type2 SAS data set in (b) into the Excel workbook Fish.xlsx in 2(c).
f. How many observations are in Type1 and Type 2 data sets in (b)?

 

My Code: 

 

libname E11 'U:\STA575\Exam1';
data one;
set E11.fish;
options firstobs = 21 obs = 35;
keep Species Weight Height;
run;
proc contents data = one;
run;

proc print data = one(firstobs = 1 obs = 5);
run;
proc print data = one;
run;
goptions reset;
data type1 type2;
set E11.fish;
if (Species ='Bream') then output type1;
if (Species='Roach') then output type2;
run;
libname E12 xlsx'U:\STA575\Exam1\Fish.xlsx';
data E12.Type1;
set type1;
run;
data E12.Type2;
set type2;
run;

kumaabi
Fluorite | Level 6

My log:

 


1 libname E11 'U:\STA575\Exam1';
NOTE: Libref E11 was successfully assigned as follows:
Engine: V9
Physical Name: U:\STA575\Exam1
2 data one;
3 set E11.fish;
4 options firstobs = 21 obs = 35;
5 keep Species Weight Height;
6 run;

NOTE: There were 55 observations read from the data set E11.FISH.
NOTE: The data set WORK.ONE has 55 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.01 seconds


7 proc contents data = one;
NOTE: Writing HTML Body file: sashtml.htm
8 run;

NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.57 seconds
cpu time 0.23 seconds


9
10 proc print data = one(firstobs = 1 obs = 5);
11 run;

NOTE: There were 5 observations read from the data set WORK.ONE.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.02 seconds
cpu time 0.03 seconds


12 proc print data = one;
13 run;

NOTE: There were 15 observations read from the data set WORK.ONE.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds


14 goptions reset;
15 data type1 type2;
16 set E11.fish;
17 if (Species ='Bream') then output type1;
18 if (Species='Roach') then output type2;
19 run;

NOTE: There were 15 observations read from the data set E11.FISH.
NOTE: The data set WORK.TYPE1 has 15 observations and 4 variables.
NOTE: The data set WORK.TYPE2 has 0 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds


20 libname E12 xlsx'U:\STA575\Exam1\Fish.xlsx';
NOTE: Libref E12 was successfully assigned as follows:
Engine: XLSX
Physical Name: U:\STA575\Exam1\Fish.xlsx
21 data E12.Type1;
22 set type1;
WARNING: FIRSTOBS option > number of observations in WORK.TYPE1.
23 run;

NOTE: There were 0 observations read from the data set WORK.TYPE1.
NOTE: The data set E12.Type1 has 0 observations and 4 variables.
NOTE: The export data set has 0 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.08 seconds
cpu time 0.04 seconds


24 data E12.Type2;
25 set type2;
WARNING: FIRSTOBS option > number of observations in WORK.TYPE2.
26 run;

NOTE: There were 0 observations read from the data set WORK.TYPE2.
NOTE: The data set E12.Type2 has 0 observations and 4 variables.
NOTE: The export data set has 0 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.07 seconds
cpu time 0.06 seconds

 

Kurt_Bremser
Super User

OPTIONS is a global statement; all settings you define with it will be effective until you change them (or the end of the SAS session).

Therefore the

options firstobs = 21 obs = 35;

is also still in effect when you try to read from TYPE1 which only has 15 observations.

It is better to use FIRSTOBS= and OBS= dataset options, as these are only valid for the data step where they are used.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1299 views
  • 2 likes
  • 3 in conversation