Presentation is loading. Please wait.

Presentation is loading. Please wait.

SAS Programming II Matthew A. Lanham Doctoral Student

Similar presentations


Presentation on theme: "SAS Programming II Matthew A. Lanham Doctoral Student"— Presentation transcript:

1 SAS Programming II Matthew A. Lanham Doctoral Student
September 17, 2018 SAS Programming II Matthew A. Lanham Doctoral Student Virginia Polytechnic Institute and State University Pamplin College of Business Department of Business Information Technology

2 September 17, 2018

3 Short Course Outline Load in Data Sets From Yesterday
SAS Programming II September 17, 2018 Short Course Outline Load in Data Sets From Yesterday Changing Data From Numeric to Character and Vise-Versa Review of DATA and PROC steps Data Example: National Longitudinal Mortality Survey PROC TTEST, PROC NPAR1WAY, PROC FREQ SQL PROC SQL Basic Plotting Examples SAS Macros References September 17, 2018 05:28

4 Lets get our Data Sets From Yesterday Loaded
Load Data SAS Programming II September 17, 2018 Lets get our Data Sets From Yesterday Loaded Drag your SAS_LISA_2.sas code into the Enhanced Editor Window Highlight lines 1 through 132 Select Run September 17, 2018 05:28

5 Changing Data Types – PUT
SAS Programming II September 17, 2018 Changing Data Types – PUT You can convert a Numeric variable to a Character variable by using the PUT function: General Form: new_variable = PUT(current_variable, new_format); DROP current_variable; RENAME new_variable = current_variable; September 17, 2018 05:28

6 Changing Data Types – INPUT
SAS Programming II September 17, 2018 Changing Data Types – INPUT You can convert a Character variable to a Numeric variable by using the INPUT function: General Form: new_variable = INPUT(current_variable, new_format); DROP current_variable; RENAME new_variable = current_variable; September 17, 2018 05:28

7 National Longitudinal Mortality Survey
NLMS SAS Programming II September 17, 2018 National Longitudinal Mortality Survey Survey given by the Census Bureau. Public access data - This is the 3rd release (June 1, 2008) 38 variables; 988,346 observations September 17, 2018 05:28

8 Lets see what our NLMS data says about this…
Using NLMS data SAS Programming II September 17, 2018 Lets see what our NLMS data says about this… Probably a meta-analysis 60 years!! That’s a long time ago September 17, 2018 05:28

9 Two-sample t-test SAS Programming II September 17, 2018 PROC TTEST A two-sample t-test allows us to test for statistical significance between two groups. Standard assumptions: (1) IID (“random”) samples, (2) Normally distributed, (3) Equal variances General Form: Two-sided test: 𝐻 1 : 𝜇 𝑚𝑎𝑟𝑟𝑖𝑒𝑑_𝑚𝑎𝑙𝑒 ≠ 𝜇 𝑢𝑛𝑚𝑎𝑟𝑟𝑖𝑒𝑑_𝑚𝑎𝑙𝑒 One-sided test: 𝐻 1 : 𝜇 𝑚𝑎𝑟𝑟𝑖𝑒𝑑_𝑚𝑎𝑙𝑒 > 𝜇 𝑢𝑛𝑚𝑎𝑟𝑟𝑖𝑒𝑑_𝑚𝑎𝑙𝑒 PROC TTEST data=libref.filename; CLASS <variable that classifies set into two groups> VAR <variable that is measured> RUN; September 17, 2018 05:28

10 PROC TTEST Assumptions: (1) IID (“random”) samples
Two-sample t-test SAS Programming II September 17, 2018 PROC TTEST Assumptions: (1) IID (“random”) samples (2) Normally distributed could try non-parametric test (3) Equal variances Use unequal variance test (Satterthwaite) Since we have unequal variances, we would make any conclusions using the unequal t-test The F-test is what you use to test for equal variances: 𝐻 1 : 𝜎 𝑚𝑚 = 𝜎 𝑢𝑚 𝑣𝑠. 𝐻 1 : 𝜎 𝑚𝑚 ≠ 𝜎 𝑢𝑚 September 17, 2018 05:28

11 PROC FREQ or PROC NPAR1WAY
Wilcoxon rank sum test SAS Programming II September 17, 2018 PROC FREQ or PROC NPAR1WAY Parametric methods of statistical inference require you to assume that your data come from some underlying probability distribution (in our case Normal) Nonparametric methods relax underlying assumptions about how the data is generated, because maybe you don’t know or the parametric assumptions are not satisfied. Nonparametric equivalent to two-sample t-test is: Wilcox rank sum test (Wilcoxon-Mann-Whitney Test) Standard assumptions: (1) I hope this works or my dissertation is shot! General Forms: PROC FREQ data=libref.filename <options>; TABLES group*measure / <options>; RUN; PROC NPAR1WAY data=libref.filename <options>; CLASS <variable that classifies set into two groups> VAR <variable that is measured> RUN; September 17, 2018 05:28

12 PROC FREQ and PROC NPAR1WAY
Wilcoxon rank sum test SAS Programming II September 17, 2018 PROC FREQ and PROC NPAR1WAY 𝐻 1 :𝑑𝑖𝑠 𝑡 ′ 𝑛 𝑜𝑓 𝑚𝑚≠𝑑𝑖𝑠𝑡𝑛 𝑜𝑓 𝑢𝑚 We can conclude there is a difference in life expectancy among married men versus unmarried men. September 17, 2018 05:28

13 Me in 20 years… Pursue Industry
Old age.. SAS Programming II September 17, 2018 Me in 20 years… Looks like Kim has a lot to look forward to either way. Pursue Industry Academic Life September 17, 2018 05:28

14 SQL SQL (pronounced “SEQUEL”) means Structured Query Language
SAS Programming II September 17, 2018 SQL SQL (pronounced “SEQUEL”) means Structured Query Language The language used by relational database management systems (RDMS) Examples: Microsoft Access, MySQL, Oracle, PostgreSQL, SQL Server,… General Form: Example: Database Table called Employee SELECT <specifies the columns to be selected> FROM <database table or libref.libname to be queried> WHERE <subsets the data on a condition> GROUP BY <variables used in functions> HAVING <a filter for thos e variables in the GROUP BY> ORDER BY <variables to order by> Note: You will always have at least a SELECT and a FROM SELECT Name, Salary FROM Employee WHERE ID >=200 AND Salary >48000 September 17, 2018 05:28

15 PROC SQL SAS Programming II September 17, 2018 PROC SQL PROC SQL allows you to treat your SAS Data Sets like Database tables. General Form: Does not require a RUN; PROC SQL; SELECT <specifies the columns to be selected> FROM <database table or libref.libname to be queried> WHERE <subsets the data on a condition> GROUP BY <variables used in functions> HAVING <a filter for thos e variables in the GROUP BY> ORDER BY <variables to order by>; QUIT; Select * means select all columns Instead of desc, you could also use asc September 17, 2018 05:28

16 Creating a New Variable or New Table
PROC SQL SAS Programming II September 17, 2018 Creating a New Variable or New Table Healthy average male weight (5’11, 65+) is approximately 181 pounds. Here we can create a new variable called “pct_above_avg” using AS: General Form: PROC SQL; CREATE TABLE libref.libname AS SELECT <specifies the columns to be selected> FROM <database table or libref.libname to be queried> …. QUIT; September 17, 2018 05:28

17 Querying Multiple Tables
PROC SQL SAS Programming II September 17, 2018 Querying Multiple Tables PROC SQL allows you to examine data stored in two tables. Combining tables horizontally is called “joining.” Joins do not alter tables! General Form: PROC SQL; SELECT libname.variable2, libname.variable2 FROM libref.libnameA, libref.libnameB WHERE libnameA.variable1 = libnameB.variable1; QUIT; A lisa.B September 17, 2018 05:28

18 Querying Multiple Tables
PROC SQL SAS Programming II September 17, 2018 Querying Multiple Tables This example also shows how to do a many-to-many match merge which is not available using the standard MERGE in a DATA step. General Form: PROC SQL; SELECT libname.variable2, libname.variable2 FROM libref.libnameA, libref.libnameB WHERE libnameA.variable1 = libnameB.variable1; QUIT; September 17, 2018 05:28

19 PROC SQL SAS Programming II September 17, 2018 Group By Use a Group By when you are performing some aggregation function in the SELECT clause like sum(), count(), avg() September 17, 2018 05:28

20 Basic plotting Examples
SAS Programming II September 17, 2018 Basic plotting Examples SGPLOT makes some nice plots. Here are couple quick examples. Histogram Side-by-side Box Plots September 17, 2018 05:28

21 SAS Macros General Form: When using %LET:
SAS Programming II September 17, 2018 SAS Macros SAS macro variables enable you to substitute text in your SAS programs. When you reference a macro variable in a SAS program, SAS replaces the reference with the text value that has been assigned to that macro variable. This in turn will enable your programs to become more reusable and dynamic. The first thing you want to do is create a LET statement. General Form: When using %LET: All values are stored as character strings Mathematical expressions are not evaluated The case of the value is preserved Quotation marks that enclose literals are stored as part of the value Leading and trailing blanks are removed from the value before assignment is made %LET variable =value; September 17, 2018 05:28

22 SAS Macros General Form: When using %PUT:
SAS Programming II September 17, 2018 SAS Macros A %PUT statement writes text to the SAS log. General Form: When using %PUT: Writes only to the SAS log Always writes to a new log line, starting in column one Writes blank line if text is not specified Does not require quotation marks around text Can be used either inside or outside a macro definition The %INDEX function – enables you to determine the position of the first character of a string within another string. %PUT text; September 17, 2018 05:28

23 Macros SAS Programming II September 17, 2018 SAS Macros Like macro variables, macro programs (macros) enable you to substitute text into your programs. Macros are different from macro variables because they use conditional logic to make decisions about the text that you substitute into your programs. You use a macro program you must first define it with a %MACRO statement, then compile it. General Form: %MACRO macro_name; text %MEND macro_name; September 17, 2018 05:28

24 SAS Macros General Form:
SAS Programming II September 17, 2018 SAS Macros You can also pass in parameters to your macro programs. You would first define and compile it like before. General Form: Side note: You will notice that your Results Viewer fills up rather quickly while you are working. If you want to delete everything in there you may find this code helpful. %MACRO macro_name(parameter1=, parameter2=,..); text %MEND; September 17, 2018 05:28

25 DATA set manipulation SAS Programming II September 17, 2018 Loops In SAS Programming I we learned IF-THEN-ELSE statement syntax which allowed us to execute one statement. Recall… To execute multiple statements, we can implement a loop using DO and END statements. General Form: IF <expression 1> THEN <statement>; ELSE IF <expression2> THEN <statement2>; ELSE <statement3>; IF <expression 1> THEN DO; <statement>; END; ELSE DO; <statement2>; September 17, 2018 05:28

26 Macros SAS Programming II September 17, 2018 SAS Macros When you submit SAS programs with macros it goes to macro processor, which generates SAS code from the macro references. SAS then compiles your code and executes your program. Oddly, not until final execution does SAS finally see any actual data values. CALL SYMPUT(macro_var_name, value) takes a value from a DATA step and assigns it a macro variable. Note: You will need to convert numeric to character and trim blanks using this approach. September 17, 2018 05:28

27 References September 17, 2018 References SAS Programming II


Download ppt "SAS Programming II Matthew A. Lanham Doctoral Student"

Similar presentations


Ads by Google