Download presentation
Presentation is loading. Please wait.
Published byTiffany Welch Modified over 6 years ago
1
Lesson 12 Topics Macro example Exporting data Character Functions
Background to final assignment Welcome to lesson 12, the last lesson for the class. In this lesson we will briefly touch on several topics, some of which you may find useful in your SAS programming future. We will look at more SGPLOT examples for line graphs so you have examples all in one place; we will then see how to export SAS datasets to other applications such as Excel and to other computers. We will cover some basic examples of using a table generating procedure call proc tabulate and finally give a brief introduction to macros and macro variables.
2
proc tabulate data=tomhs noseps; class group; var dbp12;
| | Diastolic BP at 12-Months | | | | | | N | Mean | Std | Min | Max | | | |Study Group (1-6) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |All | | | | | | proc tabulate data=tomhs noseps; class group; var dbp12; table (group all), (dbp12)*(n*f=7.0 mean*f=7.1 std*f=7.1 min*f=7.1 max*f=7.1)/rts=30; run;
3
MACRO BRKSPSS: Creates tabulate table for each var in dlist by group
%macro brkspss (grp,dlist,data=_last_,dec=3,all=all); %do I = 1 %to 100; %let depvar = %scan(&dlist,&i); %let %length(&depvar) = 0 %then %goto done; proc tabulate data=&data noseps; class &grp; var &depvar; table (&grp &all), (&depvar)*(n*f=7.0 mean*f=7.&dec std*f=7.&dec min*f=7.&dec max*f=7.&dec)/rts=30; run; %end; %done: %mend brkspss; %brkspss(group,dbp12 sbp12 chol12);
4
MACRO BRKSPSS: Creates tabulate table for each var by group
LIBNAME t '~/PH6420/2017/Data/'; DATA stat; set t.tomhs; RUN; * Example calls; %brkspss(group,dbp12 sbp12 chol12); %brkspss(group,dbp12 sbp12 chol12, dec=1); * Just 1-decimal; %brkspss(group,dbp12 sbp12 chol12, all=); * No totals;
5
Output from last call: First 2 variables.
| | Diastolic BP at 12-Months | | | | | | N | Mean | Std | Min | Max | | | |Study Group (1-6) | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Systolic BP at 12-Months | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
6
At beginning of program before you call it
Where to put macro? At beginning of program before you call it %macro brkspss(parameters); … macro code %mend brkspss; data tomhs; set t.tomhs; run; %brkspss (group, dbp12 sbp12, data=tomhs); Save as separate sas file and %include file on top of program. %include ‘/folderpath/brkspss.sas’; %brkspss(group, dbp12 sbp12, data=tomhs);
7
%condesf macro: Very Useful
Condescriptive of: tomhscv (Obs=902, Nvar=15 Created: 27NOV17 15:52) Seq Name T Format Variable Label N Mean Std Dev Minimun Maximum 1 ptid C Patient ID A D02136 2 group N Study Group (1-6) 3 age N Age (y) at Randomization 4 sex N =Male 2=Female 5 eversmk N Ever Smoke Cigarettes (1=Y, 2=N) 6 nowsmk N Now Smoke Cigarettes (1=Y, 2=N) 7 sbpbl N Systolic BP at Baseline 8 sbp N Systolic BP at 12-Months 9 cholbl N Total Cholesterol at Baseline 10 chol N Total Cholesterol at 12-Months 11 hdlbl N HDL Cholesterol at Baseline 12 hdl N HDL Cholesterol at 12 Months 13 glucosbl N Blood Glucose at Baseline 14 glucos N Blood Glucose at 12 Months 15 cvd N Cardiovascular Event (1=yes,2=no)
8
Steps to Using %condesf
Download the macro from class website Place macro in folder Use %include to make the macro available Call the macro
9
Using %condesf libname t ‘/folders/myfolders/’;
%include ‘/folders/myfolders/condesf.sas’; * if using SAS University edition; %condesf(t.tomhscv,folder=/folders/myfolders/); * Generates two files: tomhscv.condes and tomhs.pdf;
10
LIBNAME t ‘C:\SAS_Files’; DATA tomhs; SET t.tomhs;
* Exporting Data; LIBNAME t ‘C:\SAS_Files’; DATA tomhs; SET t.tomhs; KEEP ptid clinic randdate group educ wt12 sbp12; RUN; * Export data to a comma delimited file; PROC EXPORT DATA=tomhs OUTFILE = 'C:\SAS_Files\tomhs.csv' DBMS = csv REPLACE; There are times when you might want to export a SAS dataset to another format so that other software can access the data. An example would be sending your dataset to excel so you can use Excel to create some graphics (for users not familiar with SAS SGPLOT!) You can do this using PROC EXPORT, the compliment procedure of PROC IMPORT. Here we export several variables from the sescore dataset created previously. We use a short DATA step to create a dataset called temp, reading sescore and keeping variable listed in the KEEP statement. In the PROC EXPORT we output the dataset temp to a CSV file, naming the file se.csv. The DBMS option is set to csv, which can be omitted if we use the csv extension on the output file. The replace option tells SAS to overwrite the export file if it already exists.
11
Contents of file ‘tomhs.csv'
ptid,clinic,randdate,group,educ,wt12,sbp12 A00083,A,02/05/1987,2,7,125,113 A00301,A,02/17/1987,6,9,,, A00312,A,04/08/1987,3,4,131,113 This file can be read by other software program, e.g. excel or R. If you view the exported file it should look like what is displayed here. The first row will contain the variable names. You see the data is comma delimited and that missing data is written as multiple commas. This file can be opened in excel by clicking on the file. You can then save the file as an excel worksheet. In PC SAS, You can also export the SAS dataset directly to an excel file by using an xls extension on the output file. The DBMS is then set to excel, or can be omitted if the xls extension is used.
12
Moving a SAS Dataset to another computer
Transfer SAS dataset directly - Easy and works on most systems - Can send as attachment Use PROC CPORT and PROC CIMPORT Works on all systems but requires you to create an xport (.xpt) file. Can transfer multiple datasets in one file. You may want to send a SAS dataset to another computer for you or another person to use. First of all, SAS must be installed on the other computer, otherwise it will do them no good. In most cases you can transfer the dataset by (as an attachment) or ftp. This will work for PC and UNIX systems. The person on the other end saves the attached file and is ready to go, using an appropriate LIBNAME statement in their SAS program to point to the file. In some cases you will first need to create what is called a SAS export file. One advantage of doing this is that the export file can hold multiple SAS datasets. You create an export file using PROC CPORT. Then after the export file is sent to the other computer the SAS datasets are extracted using PROC CIMPORT.
13
Creating a SAS Export File
* Run this on the your computer ; LIBNAME mylib ‘C:\SAS_Files'; FILENAME tranfile ‘C:\SAS_Files\classdata.xpt'; PROC CPORT LIB=mylib FILE=tranfile; SELECT sescore tomhs; RUN; * Run this on the other computer ; FILENAME tranfile 'C:\My SAS Datasets\classdata.xpt'; PROC CIMPORT LIB=work FILE=tranfile; PROC CONTENTS VARNUM DATA=sescore; PROC CONTENTS VARNUM DATA=tomhs; RUN; Here we illustrate the call to PROC CPORT. The example shown here creates an export file of two datasets, sescore and tomhsp. PROC CPORT takes these datasets located in the mylib folder (pointing to ‘C:\SAS_Files’) and writes out these datasets to the export file called classdata.xpt. XPT is the file extension for SAS export files. This can be then be sent to the other computer or user. The person on the other end saves the file and uses PROC CIMPORT to extract the SAS dataset. The example here will extract the datasets to the work folder. To save them permanently a different libname would need to be give. You will then usually run a proc contents on the new datasets to see what you “got”.
14
* Character functions;
Start with 3 names all in caps: fname = GREGORY lname = GRANDITS mi = A Create a new variable fullname = Gregory A. Grandits In some cases you may be working with character data such as names, addresses, etc. Often times the data will be entered into the computer as separate variables for say the first, middle initial, and last names. However, for some reports or listings you may want to have the entire name be one variable as shown here for my name. SAS has several character functions that can help you do this task. 14
15
Functions/Operators SUBSTR Takes a subset of characters from a character variable LOWCASE Changes characters to lower case (also UPCASE and PROPCASE) || Concatenates variables or strings var1 = 'abc'; var2 = 'def'; var3 = var1||var2; var3 has value 'abcdef‘ CAT (CATX) Functions that concatenate vars/strings SCAN Picks off “words” from a char variable Section 3.3 LSB Cody and Smith have an entire chapter covering character functions. If you are doing a lot of work with character data you may want to read through the entire chapter. I will illustrate a few character functions that help do common tasks. The first is the SUBSTR function which takes a subset of characters from a variable and places them into a new variable. The LOWCASE function changes all upper case letters in a variable to lower case. There is also an upper case function and a proper case function. The latter capitalizes the first character of each word and makes lower case all other letters. The || is the concatenate operator. It is used to “concat” character variables or strings together as illustrated here. The CAT function is newly added to SAS; it can replace the concatenate operator. There are several other CAT type functions; one CATX inserts a character in between each variable or string. We will use that in program 10. The SCAN function picks off words based on word boundaries where boundaries are blanks or other special characters. 15
16
INFORMAT fname $20. lname $20. mi $1. ; INPUT lname fname mi ;
DATA names; INFILE DATALINES DSD; INFORMAT fname $20. lname $20. mi $1. ; INPUT lname fname mi ; LENGTH fnamemix $20. lnamemix $20. fullname $44.; fnamemix = PROPCASE(fname); lnamemix = PROPCASE(lname); miperiod = CAT(mi,'.'); fullname = CATX(' ',fnamemix,miperiod,lnamemix); DATALINES; GRANDITS, GREGORY, A SIU, YI, W ; Obs fnamemix lnamemix miperiod fullname 1 Gregory Grandits A Gregory A. Grandits 2 Yi Siu W Yi W. Siu Let’s see how some of these functions are used. In program 10 we read in the last name, first name, and middle initial (there are just 2 rows). Our goal is to create a single variable containing the names, changing upper case characters that are not the first character of a name to lower case. We also want to add a period to the middle initial. We start by computing a new variable called fnamemix. This will contain the first name in mixed case. This is done by using the PROPCASE function. The same function is used for the last name to create the last name as mixed case. We use the cat function to add a period to the middle initial. The result is stored in the variable miperiod. Finally, we use the CATX function to concatenate the three names, inserting a blank in-between each name. All other blanks are removed. The length statements are used to define the maximum number of characters the variable can contain. SAS will sometimes make the concatenated new variable much longer than they need to be. It is usually a good idea to set the length of a character variable upfront to be the largest number of characters the variable could be. Take a little time to study these example. Working with character variables like this can be a little intimidating, although less so with some of the new SAS functions. . 16
17
LENGTH fname $20. lname $20. mi $2.;
* Start with one variable for full name but wish to create separate variables for each. Use the SCAN function ; DATA names; INFILE DATALINES DSD; INFORMAT fullname $44.; INPUT fullname ; LENGTH fname $20. lname $20. mi $2.; fname = SCAN(fullname,1); *Take 1st word; mi = SCAN(fullname,2,' '); *Take 2nd word; lname = SCAN(fullname,3); *Take 3rd word; DATALINES; Gregory A. Grandits Yi W. Siu ; Sometimes you want to go in the other direction, i.e. the full name is contained in one variable and you would like to make each part of the name a separate variable. This is fairly easy to do using the SCAN function. The SCAN function picks off “words” where words are defined by delimiters. Here fname is the first word of fullname, mi is the second word, and lname is the third word. Since by default a period is a delimiter, we need to specify for the middle initial that just a blank should be used as a delimiter. Otherwise you would not get the period as part of the middle initial. 17
18
PROC PRINT DATA=names; VAR fullname fname mi lname;
TITLE 'Original Variable and new variables'; RUN; Obs fullname fname mi lname 1 Gregory A. Grandits Gregory A. Grandits 2 Yi W. Siu Yi W. Siu Here is a display of each of the variables. You see that we have correctly separated the name into three variables. As mentioned Cody and Smith have an entire chapter on character functions. Much of that you may never need but if you find yourself with a task of combining and separating character variables, you may want to refer to that chapter and the program we just covered. 18
19
Background to Final Assignment
Assessing individual risk of CVD is important to determining treatment options Age, blood pressure, cholesterol, smoking, and diabetes are risk factors for CVD The Framingham study has baseline RF and long-term follow-up for CVD that allows estimating long-term risk based on baseline RF
20
Framingham Equation Uses baseline smoking, systolic BP, total cholesterol, HDL-C, and diabetes status to quantify risk of CVD Uses Cox-regression (similar to logistic regression) with above RF in model to estimate CVD risk. Plug in above RF values into formula to estimate probability of CVD in 10-years.
21
Framingham Equation (Formula for Women)
2.33 * log of age + 1.21 * log of total cholesterol - 0.71 * log HDL cholesterol + 2.76 * log systolic BP + 0.53 * smoking (0 or 1) + 0.69 * diabetes (0 or 1) Compute total score for individual Average total score = 26.19
22
Framingham Equations (Formula for Women)
difference= (total score for person – average score) P = 1 – 0.95exp(difference) Simple example: If total score = average score P = 1 – 0.95exp(0) = 1 – 0.95 = 0.05
23
Framingham Equation (Example for Women)
age = 61 total cholesterol = 180 HDL-C = 47 Systolic BP = 124 Current smoker (1) Not diabetic (0) Score = 2.33 * log(61) + 1.21 * log(180) – 0.71 * log(47) + 2.76 * log(124) + 0.53 * 1 + 0.69 * 0 = 26.97 p = exp( ) =
24
SAS Functions Needed log = natural log exp = exponentiation
** = raised to a power Score = 2.33*log(age) + … ; Risk = 1 – 0.95**exp(score – average score);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.