I am a noob and i would like to know if my code could be cleaned up. I am using SASUniverstiyEdition. I am having a bit of trouble in the last part of my assignment which asks to use the WHERE statement to yield results, preventing certain variables from being read and written. Feels messy but it works. Here is my code, and following will be what is asked for the assignment:
OPTIONS NODATE NONUMBER PAGESIZE=20;
DATA Femaleds;
INFILE DATALINES FIRSTOBS=3;
INPUT Name $15. @17 School $ Gender $ Major $ GPA Degree $
Stipend DOLLAR10.2 Graduation MMDDYY11.;
DATALINES;
Variables: Name School Gender Major GPA Degree Stipend Graduation
----+----1----+----2----+----3----+----4----+----5----+----6
Lisa Adams PSU F MATH 2.50 BS $1,500.00 08/01/2009
Sharon Boone PSU F MATH 3.20 BS $1,500.00 12/15/2010
Tanya Redding UNC F CHEM 1.50 BS $1,000.00 08/01/2012
Denise Austin NCSU F BUSADM 2.00 MBA $0.00 08/01/2008
Julia Mann LSU F COMPSC 2.74 PHD $0.00 12/15/2009
Joyce Johnson YALE F MATH 3.65 PHD $3,000.00 12/15/2012
Tanya Jacks LSU F BUSADM 3.10 MBA $500.00 08/01/2012
Beverly Hook YALE F MATH 4.00 BA $0.00 08/01/2008
Elise Booker PSU F COMPSC 2.77 BS $0.00 12/15/2008
;
RUN;
PROC SORT DATA=Femaleds;
BY GPA;
RUN;
PROC PRINT DATA=Femaleds;
FORMAT Name $15. School $4. Gender $1. Major $6. GPA 4.2 Degree $3.
Stipend DOLLAR10.2 Graduation MMDDYY10.;
VAR Name School Gender Major GPA Degree Stipend Graduation;
TITLE1 'Female College Data';
TITLE2 'Program Exercize 05';
FOOTNOTE1 'SAS Programming (Online)';
FOOTNOTE2 'Verify Input Data';
RUN;
DATA Maleds;
INFILE DATALINES FIRSTOBS=3;
INPUT Name $15. @17 School $ Gender $ Major $ GPA Degree $
Stipend DOLLAR10.2 Graduation MMDDYY11.;
DATALINES;
Variables: Name School Gender Major GPA Degree Stipend Graduation
----+----1----+----2----+----3----+----4----+----5----+----6
George Harvest NCSU M CHEM 3.60 BS $2,200.00 12/15/2009
Gregory Daniels NCSU M COMPSC 2.80 BS $0.00 12/15/2009
Harold Doggs YALE M PSYCH 3.60 BA $0.00 12/15/2010
Aaron Jones UNC M PSYCH 3.95 BA $0.00 08/01/2008
Carl Franks PSU M MATH 2.94 PHD $1,500.00 12/15/2010
Henry Todd PSU M PSYCH 3.09 BA $250.00 12/15/2008
;
RUN;
PROC SORT DATA=Maleds;
BY GPA;
RUN;
PROC PRINT DATA=Maleds;
FORMAT Name $15. School $4. Gender $1. Major $6. GPA 4.2 Degree $3.
Stipend DOLLAR10.2 Graduation MMDDYY10.;
VAR Name School Gender Major GPA Degree Stipend Graduation;
TITLE1 'Male College Data';
TITLE2 'Program Exercize 05';
FOOTNOTE1 'SAS Programming (Online)';
FOOTNOTE2 'Verify Input Data';
RUN;
DATA AllStudentsds;
SET Femaleds Maleds;
*BY GPA;
INFORMAT GenderDesc $7.;
IF Gender = 'M' THEN GenderDesc = 'Male';
ELSE IF Gender = 'F' THEN GenderDesc = 'Female';
ELSE IF Gender = . THEN GenderDesc = 'Missing';
RUN;
PROC FORMAT;
VALUE $degree
'BA'='Bachelor of Arts'
'BS'='Bachelor of Science'
'MBA'='Master of Business Administration'
'PHD'='Doctor of Philosophy';
RUN;
OPTIONS NODATE NONUMBER PAGESIZE=30 ORIENTATION=landscape;
PROC PRINT DATA=AllStudentsds;
FORMAT Name $15. School $4. Gender $1. Major $6. GPA 4.2 Degree $degree.
Stipend DOLLAR10.2 Graduation MMDDYY10. GenderDesc $7.;
VAR Name School Gender Major GPA Degree Stipend Graduation GenderDesc;
TITLE1 'All Students College Data';
TITLE2 'Program Exercize 05';
FOOTNOTE1 'SAS Programming (Online)';
FOOTNOTE2 'Combine Maleds and Femaleds Data Sets';
RUN;
PROC MEANS DATA = AllStudentsds NOPRINT;
CLASS Gender;
VAR Stipend;
OUTPUT OUT = Statisticsds SUM(Stipend) = SUMStipend;
RUN;
DATA Statisticsds;
MERGE AllStudentsds Statisticsds;
BY Gender;
RUN;
DATA Studtotsds (WHERE=(Gender));
SET AllStudentsds Statisticsds;
PCTStipend = Stipend / SUMStipend;
RUN;
OPTIONS NODATE NONUMBER PAGESIZE=40 ORIENTATION=landscape LINESIZE=160;
PROC PRINT DATA=Studtotsds;
FORMAT Name $15. School $4. Gender $1. Major $6. GPA 4.2 Degree $degree.
Stipend DOLLAR10.2 Graduation MMDDYY10. GenderDesc $7. SUMStipend DOLLAR10.2
PCTStipend PERCENT5.;
VAR Name School Gender Major GPA Degree Stipend Graduation GenderDesc SUMStipend PCTStipend;
TITLE1 'Stipend Report for Males and Females';
TITLE2 'Program Exercize 05';
FOOTNOTE1 'SAS Programming (Online)';
FOOTNOTE2 ' Final Result';
RUN;
M. Use the MEANS procedure to create a temporary data set named Statisticsds based on the guidelines listed below.
Read the input data from the temporary data set AllStudentsds
Generate totals for the values in the Stipend variable. Use a variable named SUMStipend to store the
results
a. Generate a sub-total of the Stipend values for each unique value in the Gender variable as well as a
grand-total of Stipend values for all values in the Gender variable
Output the results to a temporary data set named Statisticsds
Do not generate printed results for the MEANS procedure
N. Write the SAS PROC and DATA steps to create a temporary data set named Stutotsds from the existing AllStudentsds and Statisticsds data sets.
Use the WHERE data set option to prevent SAS from reading the observation in the Statisticsds data set that does not contain a value for the Gender variable.
Use the appropriate data set option to prevent SAS from writing the _TYPE_ and _FREQ_ variables to the Stutotsds data set.
Divide the value in the Stipend variable by the value in the SUMStipend variable and store the result in a variable named PCTStipend.
AddtheSASprogrammingstatementstoyourSASprogramtoperformtheactionslistedbelow.
Do not display the date/time at the top of each page of output
Set the maximum number of lines per page of output to 40
Change the orientation from portrait to landscape
Set the maximum length of output lines 160
Do not display the page number at the top of each page of output
Display Stipend Report for Males and Females and Program Exercise 05 as the title and sub-title, respectively. Display SAS Programming (Online) as the footer. Display Final Results as the sub-footer. Display submitted by FirstName LastName as another sub-footer. Remember to replace FirstName with your first name and LastName with your last name.
Use the PRINT procedure to display the contents of the temporary Studtotsds SAS data set. Include the appropriate statements in the PRINT procedure of your SAS program to display the contents of your data set just like the Sample Program Output for PRINT-Stutotsds.
I get this for my final results
Stipend Report for Males and Females
Program Exercize 05
Obs Name School Gender Major GPA Degree Stipend Graduation GenderDesc SUMStipend PCTStipend
1 Tanya Redding UNC F CHEM 1.50 Bachelor of Science $1,000.00 08/01/2012 Female . .
2 Denise Austin NCSU F BUSADM 2.00 Master of Business Administration $0.00 08/01/2008 Female . .
3 Lisa Adams PSU F MATH 2.50 Bachelor of Science $1,500.00 08/01/2009 Female . .
4 Julia Mann LSU F COMPSC 2.74 Doctor of Philosophy $0.00 12/15/2009 Female . .
5 Elise Booker PSU F COMPSC 2.77 Bachelor of Science $0.00 12/15/2008 Female . .
6 Tanya Jacks LSU F BUSADM 3.10 Master of Business Administration $500.00 08/01/2012 Female . .
7 Sharon Boone PSU F MATH 3.20 Bachelor of Science $1,500.00 12/15/2010 Female . .
8 Joyce Johnson YALE F MATH 3.65 Doctor of Philosophy $3,000.00 12/15/2012 Female . .
9 Beverly Hook YALE F MATH 4.00 Bachelor of Arts $0.00 08/01/2008 Female . .
10 Gregory Daniels NCSU M COMPSC 2.80 Bachelor of Science $0.00 12/15/2009 Male . .
11 Carl Franks PSU M MATH 2.94 Doctor of Philosophy $1,500.00 12/15/2010 Male . .
12 Henry Todd PSU M PSYCH 3.09 Bachelor of Arts $250.00 12/15/2008 Male . .
13 George Harvest NCSU M CHEM 3.60 Bachelor of Science $2,200.00 12/15/2009 Male . .
14 Harold Doggs YALE M PSYCH 3.60 Bachelor of Arts $0.00 12/15/2010 Male . .
15 Aaron Jones UNC M PSYCH 3.95 Bachelor of Arts $0.00 08/01/2008 Male . .
16 Tanya Redding UNC F CHEM 1.50 Bachelor of Science $1,000.00 08/01/2012 Female $7,500.00 13%
17 Denise Austin NCSU F BUSADM 2.00 Master of Business Administration $0.00 08/01/2008 Female $7,500.00 0%
18 Lisa Adams PSU F MATH 2.50 Bachelor of Science $1,500.00 08/01/2009 Female $7,500.00 20%
19 Julia Mann LSU F COMPSC 2.74 Doctor of Philosophy $0.00 12/15/2009 Female $7,500.00 0%
20 Elise Booker PSU F COMPSC 2.77 Bachelor of Science $0.00 12/15/2008 Female $7,500.00 0%
21 Tanya Jacks LSU F BUSADM 3.10 Master of Business Administration $500.00 08/01/2012 Female $7,500.00 7%
22 Sharon Boone PSU F MATH 3.20 Bachelor of Science $1,500.00 12/15/2010 Female $7,500.00 20%
23 Joyce Johnson YALE F MATH 3.65 Doctor of Philosophy $3,000.00 12/15/2012 Female $7,500.00 40%
24 Beverly Hook YALE F MATH 4.00 Bachelor of Arts $0.00 08/01/2008 Female $7,500.00 0%
25 Gregory Daniels NCSU M COMPSC 2.80 Bachelor of Science $0.00 12/15/2009 Male $3,950.00 0%
26 Carl Franks PSU M MATH 2.94 Doctor of Philosophy $1,500.00 12/15/2010 Male $3,950.00 38%
27 Henry Todd PSU M PSYCH 3.09 Bachelor of Arts $250.00 12/15/2008 Male $3,950.00 6%
28 George Harvest NCSU M CHEM 3.60 Bachelor of Science $2,200.00 12/15/2009 Male $3,950.00 56%
29 Harold Doggs YALE M PSYCH 3.60 Bachelor of Arts $0.00 12/15/2010 Male $3,950.00 0%
30 Aaron Jones UNC M PSYCH 3.95 Bachelor of Arts $0.00 08/01/2008 Male $3,950.00 0%
I need for obs. 1-15 in these results, not to be there.
Any advise would be appreciated. This is one of my first attempts at SAS.
@QuickJohn515 wrote:
Use the WHERE data set option to prevent SAS from reading the observation in the Statisticsds data set that does not contain a value for the Gender variable.
Try this
DATA Studtotsds (WHERE=(not missing(Gender)));
Use the appropriate data set option to prevent SAS from writing the _TYPE_ and _FREQ_ variables to the Stutotsds data set.
Then try this:
DATA Studtotsds (WHERE=(not missing(Gender)
) drop=_type _freq_);
or
DATA Studtotsds (WHERE=(not missing(Gender)) drop=_:);
Finally:
I need for obs. 1-15 in these results, not to be there.
Since I already showed you how to use the WHERE= data set option, you should be able to figure this out yourself.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: