SAS ROUTINES Contents 1 Label variable.........................................

SAS ROUTINES Contents 1 Label variable.......................................................................................................4 2 Report..................................................................................................................4 3 Define...................................................................................................................4 4 Reading from text file...........................................................................................4 5 Reading from csv file............................................................................................5 6 Capturing output in file........................................................................................5 6.1.1 Creating an output data set.....................................................................5 6.1.2 How to identify output objects.................................................................5 6.1.3 Using object label to create an output data set.......................................5 6.1.4 T urn the listing output of........................................................................5 6.1.5 Output to an HTML file.............................................................................5 7 95% confidence interval of mean.........................................................................9 8 Column input........................................................................................................9 9 formatted input....................................................................................................9 10 Formatting in proc report-...............................................................................10 11 Proc report......................................................................................................10 12 Across variable to group variable horizontally................................................10 13 Computed variable..........................................................................................11 14 Summary report..............................................................................................11 15 Viewing contents of dataset............................................................................11 16 printing portion of dataset..............................................................................11 17 Print by............................................................................................................11 18 create new library...........................................................................................12 19 creating and adding data to dataset...............................................................12 20 importing csv file............................................................................................12 21 importing excel file.........................................................................................13 22 IMPORTING TEXT FILE......................................................................................13 23 COPYING DATASET...........................................................................................14 24 ADDING NEW VARIABLES AND CREATING DATASET........................................14 25 DROP AND KEEP VARIABLES IN NEW DATASET................................................14 26 PRINTING NO OBSERVATION NUMBER.............................................................15 27 SUBSTRING.....................................................................................................15 28 OTHER STRING FUNCTIONS.............................................................................15 29 DATE FUNCTION..............................................................................................15 30 PRINTING VARIABLES IN DATASET...................................................................15 31 SORT...............................................................................................................16 32 REMOVING DUPLICATE....................................................................................16 33 REMOVE DUPLICATE BASED ON KEY...............................................................16 34 MOVE DUPLICATES INTO NEW DATASET..........................................................17 35 PLOT................................................................................................................17 36 Median in proc sql...........................................................................................18 37 PROC SQL........................................................................................................18 38 Proc sql case...................................................................................................19 39 MERGING TWO DATASETS...............................................................................19 40 SAMPLING.......................................................................................................20 41 PRINTING VERTICAL HEADING.........................................................................21 42 MEAN CALCULATION.......................................................................................21 43 Moving means data into output file................................................................23 44 Merging two data together..............................................................................24 45 QUANTILES......................................................................................................25 46 TO REMOVE NA VALUES TO NUMERICAL.........................................................26 47 CREATE FREQUENCY TABLE.............................................................................26 48 create two variable categorical frequency table.............................................26 49 Weight statement............................................................................................27 50 order...............................................................................................................27 51 three variable frequency.................................................................................28 52 Correlation......................................................................................................28 53 Regression......................................................................................................28 54 logistic regression...........................................................................................28 55 test stationarity...............................................................................................28 56 create a diferentiated time series..................................................................29 57 create ACF and PACF Plots..............................................................................29 58 to calculate the ESACF and SCAN function values..........................................29 59 forecast using ARIMA......................................................................................29 60 advanced mean concepts...............................................................................30 60.1 Basic mean..................................................................................................30 60.2 Selecting Analysis Variables, Analyses to be Performed by PROC MEANS , and Rounding of Results........................................................................................31 60.3 Selecting Other Analyses.............................................................................31 60.4 Step 4: Analysis with CLASS (variables).......................................................33 60.5 Step 5: Don’t Miss the Missings!..................................................................34 60.6 Survey means - How to Estimate a Ratio of Means using SAS.....................34 61 Mean and ratios..............................................................................................37 62 SAS QUESTIONS..............................................................................................38 1 Label variable Label varname = ‘label name’ 2 Report Proc report data = dataset name; Column age, weight – prints age and wieight in columns 3 Define Assign formats to variables Specify column headings and width Proc report data = dataset <options>; Define variable/ <usage><attributes><options><justification><Column- heading>; Run; 4 Reading from text file data Sample2; infile 'c:\books\statistics by example\delim.txt'; length Gender $ 1; input ID Age Gender $; run; The LENGTH statement tells SAS that the variable Gender is character (the dollar sign indicates this) and that you want to store Gender in 1 byte (the 1 indicates this). The INPUT statement lists the variable names in the same order as the values in the text file. Because you already told SAS that Gender is a character variable, the dollar sign following the name Gender on the INPUT statement is not necessary. If you had not included a LENGTH statement, the dollar sign following Gender on the INPUT statement would have been necessary. SAS assumes variables are numeric unless you tell it otherwise. 5 Reading from csv file data Sample2; infile 'c:\books\statistics by example\comma.csv' dsd; length Gender $ 1; input ID Age Gender $; run; The DSD option specifies that two consecutive commas represent a missing value and that the default delimiter is a comma. Here is the modified program 6 Capturing output in file Ods listing; Ods csvall file = “d:\ramesh\output\secind.csv”; Proc means data = mydata.loan_all mean; Var default; Class T enure; Run; ods csvall close; 6.1.1 Creating an output data set 6.1.2 How to identify output objects 6.1.3 Using object label to create an output data set 6.1.4 T urn the listing output of 6.1.5 Output to an HTML file SAS introduced the Output Delivery System (ODS) with Version 7, making output much more flexible. We show some examples using ODS here. We are going to use the data set below for the purpose of demonstration. OPTIONS nocenter; DATA hsb25; INPUT id female race ses schtype $ prog read write math science socst; DATALINES; 147 1 1 3 pub 1 47 62 53 53 61 108 0 1 2 pub 2 34 33 41 36 36 18 0 3 2 pub 3 50 33 49 44 36 153 0 1 2 pub 3 39 31 40 39 51 50 0 2 2 pub 2 50 59 42 53 61 51 1 2 1 pub 2 42 36 42 31 39 102 0 1 1 pub 1 52 41 51 53 56 57 1 1 2 pub 1 71 65 72 66 56 160 1 1 2 pub 1 55 65 55 50 61 136 0 1 2 pub 1 65 59 70 63 51 88 1 1 1 pub 1 68 60 64 69 66 177 0 1 2 pri 1 55 59 62 58 51 95 0 1 1 pub 1 73 60 71 61 71 144 0 1 1 pub 2 60 65 58 61 66 139 1 1 2 pub 1 68 59 61 55 71 135 1 1 3 pub 1 63 60 65 54 66 191 1 1 1 pri 1 47 52 43 48 61 171 0 1 2 pub 1 60 54 60 55 66 22 0 3 2 pub 3 42 39 39 56 46 47 1 2 3 pub 1 47 46 49 33 41 56 0 1 2 pub 3 55 45 46 58 51 128 0 1 1 pub 1 39 33 38 47 41 36 1 2 3 pub 2 44 49 44 35 51 53 0 2 2 pub 3 34 37 46 39 31 26 1 4 1 pub 1 60 59 62 61 51 ; RUN;  Creating an output data set Let's say we have a data set of student scores and want to conduct a paired t-test on writing score and math score for each program type. For some reason, we want to save the t-values and p- values to a data set for later use. Without ODS, it would not be an easy thing to do since proc ttest does not have an output statement. With ODS it is only one more line of code. We will sort the data set first by variable prog and use statement ods output Ttests=test_output to create a temporary data set called test_output containing information of t-values and p-values together with degrees of freedom for each t-test conducted. proc sort data=hsb25; by prog; proc ttest data=hsb25; by prog; paired write*math; ods output Ttests=ttest_output; run; proc print data=ttest_output; run; The SAS System Obs prog Variable1 Variable2 Difference tValue DF Probt 1 1 write math write - math -1.57 14 0.1389 2 2 write math write - math 0.66 4 0.5475 3 3 write math write - math -2.37 4 0.0766  How to identify output objects For each SAS procedure, SAS produces a group of ODS output objects. For example, in the above example, Ttests is the name of a such object associated with proc ttest. In order to know what objects are associated with a particular proc, we use ods trace on statement right before the proc and turn the trace off right after it. Let's look at another example using proc reg. The option listing with ods trace on displays the information of an object along with the corresponding output. Below we see three objects (data sets in this case) associated with proc reg when no extra options used. The ANOVA part of the output is stored in a data set called ANOVA. The parameter estimates are stored in ParameterEstimates. Each object has a name, a label and a path along with its template. Once we obtain the name or the label of the object, we can use ods output statement to output it to a dataset as shown in the example above. ods trace on /listing; proc reg data=hsb25; model write = female math; run; quit; ods trace off; The REG Procedure Model: MODEL1 Dependent Variable: write Output Added: ------------- Name: ANOVA Label: Analysis of Variance Template: Stat.REG.ANOVA Path: Reg.MODEL1.Fit.write.ANOVA ------------- Analysis of Variance Sum of Mean Source DF Squares Square F Value Pr > F Model 2 2154.11191 1077.05596 19.39 <.0001 Error 22 1222.04809 55.54764 Corrected Total 24 3376.16000 Output Added: ------------- Name: FitStatistics Label: Fit Statistics Template: Stat.REG.FitStatistics Path: Reg.MODEL1.Fit.write.FitStatistics ------------- Root MSE 7.45303 R-Square 0.6380 Dependent Mean 50.44000 Adj R-Sq 0.6051 Coeff Var 14.77603 Output Added: ------------- Name: ParameterEstimates Label: Parameter Estimates Template: Stat.REG.ParameterEstimates Path: Reg.MODEL1.Fit.write.ParameterEstimates ------------- Parameter Estimates Parameter Standard Variable DF Estimate Error t Value Pr > |t| Intercept 1 7.07533 7.56161 0.94 0.3596 female 1 5.95697 3.07209 1.94 0.0654 math 1 0.76991 0.14323 5.38 <.0001  Using object label to create an output data set Along with the name of an object, we also see the label for the object. We can use the label to create a data set just as using the name. ods output "Parameter Estimates"=parest; uploads/Geographie/ sas-routines-guide.pdf

  • 14
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager