SAS macros to automate / simplify the process of capability analysis performed in SAS.
This SAS code defines several functions for working with significant figures and reformatting numbers. Below is a summary of each function, the parameters they accept, and the output they provide:
-
Description: This function rounds a number
xto the specified number of significant figures (sf). -
Parameters:
x: The number to be rounded.sf: The desired number of significant figures.
-
Output: The rounded number with
sfsignificant figures.
-
Description: This function extracts significant digits from a number
x. -
Parameters:
x: The number from which significant digits are to be extracted.
-
Output: A character string representing the significant digits of
x.
-
Description: This function displays a number
xwith the specified number of significant figures (sf) as a character string. -
Parameters:
x: The number to be displayed.sf: The desired number of significant figures.
-
Output: A character string representation of
xwithsfsignificant figures. Trailing zeros are added if necessary.
-
Description: This function reformats a number
xby either returning the integer part ofxif it has three or more digits or applying thesigFigfunction with three significant figures toxif it has fewer than three digits. -
Parameters:
x: The number to be reformatted.
-
Output: The reformatted number.
-
Description: This macro reformats a dataset by applying formatting conditions to a specified variable.
-
Parameters:
Data_In: The name of the input dataset to be reformatted.Var: The name of the variable within the dataset to be reformatted.
-
Reformatting Logic:
- If the value of
Varis greater than 0:- If
Varis greater than or equal to 100, it is rounded down to the nearest integer using thefloorfunction. - If
Varis less than 100, it is reformatted to have three significant figures using theSigFigfunction.
- If
- If
Varis exactly 0, it remains unchanged as 0. - If
Varis less than 0:- If
Varis less than or equal to -100, it is rounded up to the nearest integer using theceilfunction. - If
Varis greater than -100, it is reformatted to have three significant figures using theSigFigfunction while preserving its negative sign.
- If
- If the value of
-
Output: The dataset specified by
Data_Inis modified in place with the variableVarreformatted according to the specified conditions. TheVarvariable is also formatted using theBEST.format. -
Usage: To use this macro, call it by passing the name of the input dataset (
Data_In) and the name of the variable to be reformatted (Var) as arguments.Example Usage:
%re_format_dataset(MyData, MyVariable);
-
Description: This macro retrieves the value from a specified cell in a dataset and assigns it to a macro variable.
-
Parameters:
Data_In: The name of the dataset from which the value will be retrieved.Line_Loc: The row index (observation number) of the cell from which the value will be retrieved.Var: The column name (variable) of the cell from which the value will be retrieved.Data_Out_Var: The name of the macro variable to which the retrieved value will be assigned.
-
Functionality:
- The macro initializes a global macro variable named
&Data_Out_Varto hold the retrieved data. - It then reads through the dataset specified by
Data_Inand, when it reaches the row specified byLine_Loc, it assigns the value of the variableVarfrom that row to the macro variable&Data_Out_Var. - The assignment is done using the
symputfunction, which assigns the value ofVarto the macro variable named inData_Out_Var.
- The macro initializes a global macro variable named
-
Output: The macro variable specified by
Data_Out_Varwill contain the value retrieved from the specified cell in the dataset. -
Usage: To use this macro, call it by passing the name of the input dataset (
Data_In), the row index (Line_Loc), the column name (Var), and the name of the macro variable to store the data (Data_Out_Var).Example Usage:
%Get_data(MyDataset, 3, MyVariable, OutputVariable);
-
Description: This macro converts a numeric variable (
Var) into a TRUE or FALSE condition and assigns the result to a macro variable (Out) based on a specified threshold. -
Parameters:
Var: The numeric variable to be evaluated.Out: The name of the macro variable where the TRUE or FALSE condition will be stored.
-
Functionality:
- The macro initializes a global macro variable named
&Outto hold the TRUE or FALSE condition. - It evaluates the value of the variable
Varagainst the threshold of 1.33. - If
Varis less than 1.33, it assigns the string "TRUE" to the macro variable&Out. Otherwise, it assigns "FALSE."
- The macro initializes a global macro variable named
-
Output: The macro variable specified by
Outwill contain either "TRUE" or "FALSE" based on whether the condition with the threshold was met. -
Usage: To use this macro, call it by passing the numeric variable (
Var) that you want to evaluate and the name of the macro variable (Out) where the TRUE or FALSE condition will be stored.Example Usage:
%Check_Condition(MyNumericVar, MyCondition);
-
Description: This macro filters a dataset (
Data_In) to retrieve a specific sentence identified bySentence_Locand stores it in a macro variable (Data_Out_Var). -
Parameters:
Data_In: The name of the dataset containing sentences.Sentence_Loc: The location or identifier of the target sentence to retrieve.Data_Out_Var: The name of the macro variable where the retrieved sentence will be stored.
-
Functionality:
- The macro creates a temporary dataset called
temp_sentenceby filtering the rows inData_Inwhere the variableSentencematches the value ofSentence_Loc. - It then checks the number of observations (
NOBS) in thetemp_sentencedataset and initializes the macro variable&Data_Out_Varto an empty string. - If there are observations in the
temp_sentencedataset (i.e.,NOBS > 0), the macro retrieves theTextcolumn from the first observation and stores it in the macro variable&Data_Out_Var.
- The macro creates a temporary dataset called
-
Output: The macro variable specified by
Data_Out_Varwill contain the retrieved sentence, or it will be an empty string if the specified sentence is not found in the dataset. -
Usage: To use this macro, call it by passing the name of the input dataset (
Data_In), the identifier of the target sentence (Sentence_Loc), and the name of the macro variable (Data_Out_Var) where the retrieved sentence will be stored.Example Usage:
%get_sentence(MyDataset, "Identifier123", MySentence);
-
Description: This macro generates a histogram plot for a variable (
Var_Con) in a dataset (Data_In) and includes control limits (LSLandUSL) as reference lines. It allows customization of various plot settings. -
Parameters:
Data_In: The name of the dataset containing the variable to be plotted.LSL: The Lower Specification Limit (control limit) for the variable.USL: The Upper Specification Limit (control limit) for the variable.Var_Con: The name of the variable to be plotted in the histogram.Title_Label: The title to be displayed on the histogram plot.Footnote: The footnote to be displayed on the histogram plot.Image_Loc: The location where the generated plot image will be saved.Image_Name: The name to be assigned to the generated plot image.DPI: The resolution (Dots Per Inch) for the generated image.
-
Functionality:
- The macro sets ODS (Output Delivery System) options to control the output format and image quality.
- It uses PROC MEANS to calculate summary statistics for the specified variable (
Var_Con) and stores them in a table calleddescriptive_table. - The macro extracts the minimum (
Min) and maximum (Max) values from thedescriptive_tableand assigns them to macro variables. - It determines suitable lower (
LOWER) and upper (UPPER) limits for the x-axis based on the specified control limits (LSLandUSL) and the minimum and maximum values. - Using PROC SGPLOT, the macro generates a histogram plot with the specified variable, fills the histogram bars with a specified color, adds reference lines for control limits, and customizes axis labels and settings.
- The generated plot is saved as an HTML image with the specified resolution and filename.
-
Output: The macro generates a histogram plot for the specified variable with control limits and saves it as an image in the specified location.
-
Usage: To use this macro, call it by passing the necessary parameters, including the dataset name, variable name, control limits, plot title, and other customization options.
Example Usage:
%Histogram_Plot(MyDataset, 10, 30, MyVariable, "Histogram of MyVariable", "Footnote Text", "C:\Output\Images\", MyHistogram, 300);
-
Description: This macro generates a histogram plot for a variable (
Var_Con) in a dataset (Data_In) and includes control limits (LSLandUSL) as reference lines. It allows customization of various plot settings. -
Parameters:
Data_In: The name of the dataset containing the variable to be plotted.LSL: The Lower Specification Limit (control limit) for the variable.USL: The Upper Specification Limit (control limit) for the variable.Var_Con: The name of the variable to be plotted in the histogram.Title_Label: The title to be displayed on the histogram plot.Footnote: The footnote to be displayed on the histogram plot.Image_Loc: The location where the generated plot image will be saved.Image_Name: The name to be assigned to the generated plot image.DPI: The resolution (Dots Per Inch) for the generated image.
-
Functionality:
- The macro sets ODS (Output Delivery System) options to control the output format and image quality.
- It uses PROC MEANS to calculate summary statistics for the specified variable (
Var_Con) and stores them in a table calleddescriptive_table. - The macro extracts the minimum (
Min) and maximum (Max) values from thedescriptive_tableand assigns them to macro variables. - It determines suitable lower (
LOWER) and upper (UPPER) limits for the x-axis based on the specified control limits (LSLandUSL) and the minimum and maximum values. - Using PROC SGPLOT, the macro generates a histogram plot with the specified variable, fills the histogram bars with a specified color, adds reference lines for control limits, and customizes axis labels and settings.
- The generated plot is saved as an HTML image with the specified resolution and filename.
-
Output: The macro generates a histogram plot for the specified variable with control limits and saves it as an image in the specified location.
-
Usage: To use this macro, call it by passing the necessary parameters, including the dataset name, variable name, control limits, plot title, and other customization options.
Example Usage:
%Histogram_Plot(MyDataset, 10, 30, MyVariable, "Histogram of MyVariable", "Footnote Text", "C:\Output\Images\", MyHistogram, 300);
-
Description: This macro generates a sentence summarizing the results of an ANOVA analysis, including the p-value and whether it is below or above a significance level (e.g., 0.05).
-
Parameters:
Ordered_Difference: The name of the dataset containing differences between groups.p_value: The p-value resulting from the ANOVA analysis.vlabel: A label or description of the variable being analyzed.
-
Functionality:
- The macro first handles the p-value formatting. If the p-value is "<.0001," it is converted to "0.0001." Otherwise, it is converted to a numeric value using the
INPUTfunction. - It then constructs an initial sentence (
Current_State) describing the ANOVA analysis and the variable of interest (vlabel) with a placeholder for the p-value. - Based on whether the p-value is less than 0.05, the macro determines whether to state that the p-value is "below" or "above" a significance level.
- The
Ordered_Differencedataset is processed to handle p-value formatting and to create a new variable calledStatementthat describes the positions being compared in the ANOVA. - Depending on the p-value condition, the
Current_Statesentence is updated to include "below" or "above" significance.
- The macro first handles the p-value formatting. If the p-value is "<.0001," it is converted to "0.0001." Otherwise, it is converted to a numeric value using the
-
Output: The macro updates the
Current_Statesentence to describe the ANOVA results, including whether the p-value is below or above a significance level. -
Usage: To use this macro, call it by passing the necessary parameters, including the dataset name containing differences between groups (
Ordered_Difference), the p-value, and a label for the variable being analyzed (vlabel).Example Usage:
%Generate_difference(MyDifferences, 0.0001, "MyVariable");
pp_capability(Data_In, Var_Con, Var_Cat, Image_Loc, Output_Excel, Threshold, LSL, USL, Encode, units = mg/100ml, Main_Title = Capability Results)
-
Description: This macro generates a PowerPoint slide prototype for displaying capability analysis results, including histograms, box plots, descriptive statistics, and textual information.
-
Parameters:
Data_In: The name of the dataset containing the analysis data.Var_Con: The continuous variable of interest.Var_Cat: The categorical variable for grouping.Image_Loc: The location where generated images will be saved.Output_Excel: The name of the Excel file containing analysis results.Threshold: The threshold for significance.LSL: The Lower Specification Limit.USL: The Upper Specification Limit.Encode: A dataset containing encoding for text replacement.units: Units of measurement for the variable (default: "mg/100ml").Main_Title: The main title for the slide (default: "Capability Results").
-
Functionality:
- The macro starts by setting up the output environment for PowerPoint and importing relevant data.
- It prepares the data for analysis and retrieves necessary values for generating the slide.
- Descriptive statistics, histograms, and box plots are generated and saved as images.
- The ANOVA analysis is run, and p-values and differences are retrieved.
- Text replacement is performed based on encoding provided in the
Encodedataset. - The slide layout is designed with titles, text, tables, and images, and all elements are placed on the slide as per the prototype design.
-
Output: The macro generates a PowerPoint slide prototype with various elements, including images, tables, and text, to display capability analysis results.
-
Usage: To use this macro, call it by passing the necessary parameters, including the dataset name, variable names, and other customization options.
Example Usage:
%pp_capability(MyData, Measurement, Group, "C:\Output\Images\", "AnalysisResults.xlsx", 0.05, 10, 30, EncodeData, "mg/100ml", "Capability Analysis");