5 Approaches to Identify Blank Columns in SAS Datasets
5 Approaches to Identify Blank Columns in SAS Datasets
Author: Sarath
Date: September 5, 2024
When working with large datasets in SAS, it’s crucial to e…
5 Approaches to Identify Blank Columns in SAS Datasets
5 Approaches to Identify Blank Columns in SAS Datasets
Author: Sarath
Date: September 5, 2024
When working with large datasets in SAS, it’s crucial to e…
Effortlessly Upcase All Variables in SAS Using PROC DATASETS
Effortlessly Upcase All Variables in SAS Using PROC DATASETS
When working with SAS datasets, ensuring consistency across variables, especially character varia…
How to Upcase All Variables in a SAS Dataset
How to Upcase All Variables in a SAS Dataset
When working with character data in SAS, you may often need to ensure that all text variables are in uppercase. Fortunately, SAS …
Dynamic macro creation is a powerful technique in SAS that allows you to generate macro variables and macros based on data content or logic at runtime. This not only simplifies repetitive tasks but also provides a way to dynamically control program flow. In this article, we’ll cover various scenarios and provide multiple examples where dynamic macro creation can be beneficial.
Imagine a scenario where you have a dataset and need to dynamically create macros to store variable names or their values. This is useful for automating variable processing tasks, such as generating reports, manipulating data, or performing analyses.
/* Create macros for each unique variable in the dataset */
proc sql;
select distinct name
into :var1-:varN
from sashelp.class;
quit;
%macro display_vars;
%do i=1 %to &sqlobs;
%put &&var&i;
%end;
%mend display_vars;
%display_vars;
Explanation: This code dynamically selects variable names from the sashelp.class
dataset and stores them in macro variables. The macro display_vars
prints out each variable name, allowing flexible processing of variables without knowing them in advance.
Let’s say you have multiple categories or groups within your data, and you need to run a set of analyses or create reports for each unique group. You can dynamically generate macros for each category, making the process scalable and automated.
/* Create macros for each unique 'sex' category in the dataset */
proc sql;
select distinct sex
into :sex1-:sexN
from sashelp.class;
quit;
%macro analyze_sex;
%do i=1 %to &sqlobs;
proc print data=sashelp.class;
where sex = "&&sex&i";
title "Listing for Sex = &&sex&i";
run;
%end;
%mend analyze_sex;
%analyze_sex;
Explanation: This example dynamically creates a macro for each unique sex
value and applies a filter in PROC PRINT
for each group, generating reports for each distinct value in the dataset.
In some cases, you need to execute different code based on the values of certain variables or the content of a dataset. Dynamic macro creation helps generate conditional logic on the fly.
/* Identify numeric variables in the dataset and generate macro code */
proc sql;
select name
into :numvar1-:numvarN
from dictionary.columns
where libname='SASHELP' and memname='CLASS' and type='num';
quit;
%macro analyze_numeric_vars;
%do i=1 %to &sqlobs;
proc means data=sashelp.class;
var &&numvar&i;
title "Analysis of &&numvar&i";
run;
%end;
%mend analyze_numeric_vars;
%analyze_numeric_vars;
Explanation: This code identifies numeric variables in the dataset and dynamically creates macro variables for each. The macro analyze_numeric_vars
runs PROC MEANS
for each numeric variable, adjusting to any changes in the dataset structure.
Dynamic macros are helpful in generating dynamic reports or exporting data where the structure or content changes frequently. You can use dynamic macros to control file names, report titles, or export paths.
/* Generate macros for each unique 'name' value and create reports */
proc sql;
select distinct name
into :name1-:nameN
from sashelp.class;
quit;
%macro create_reports;
%do i=1 %to &sqlobs;
proc print data=sashelp.class;
where name = "&&name&i";
title "Report for &&name&i";
run;
%end;
%mend create_reports;
%create_reports;
Explanation: This code dynamically creates a report for each unique name
in the dataset, with the report’s title reflecting the name
being processed. The code adapts to changes in the dataset, automating the report generation process.
PROC SQL
with INTO
Clauses: This is the most efficient way to generate dynamic macro variables from dataset content.&sqlobs
: The &sqlobs
macro variable is useful for counting the number of records or unique values, ensuring the loop runs the correct number of times.Dynamic macro creation in SAS provides a robust and flexible way to automate repetitive tasks, process data efficiently, and adjust code dynamically based on dataset content. By generating macros based on variables or values within a dataset, you can create dynamic, scalable solutions for various SAS programming challenges.
VISIT
and VISITNUM
Values from SDTM DatasetsAuthor: [Your Name]
Date: [Creation Date]
In clinical trials, the VISIT
and VISITNUM
variables are key identifiers for subject visits. Ensuring that all datasets have consistent visit data and that it aligns with the planned visits recorded in the TV
(Trial Visits) domain is crucial for accurate data analysis. This post presents a SAS macro that automates the extraction of unique VISIT
and VISITNUM
values across all SDTM datasets in a library and compares them to those found in the TV
domain.
The SAS macro program:
VISIT
and VISITNUM
values from all SDTM datasets in the specified library.TV
domain.TV
domain.Here’s the macro that performs the task:
%macro compare_visit(libname=);
/* Step 1: Get the unique VISIT and VISITNUM values from the TV domain */
proc sql;
create table tv_visit as
select distinct VISIT, VISITNUM
from &libname..TV
where VISIT is not missing and VISITNUM is not missing;
quit;
/* Step 2: Get the list of datasets in the library containing both VISIT and VISITNUM */
proc sql noprint;
select memname
into :dslist separated by ' '
from sashelp.vcolumn
where libname = upcase("&libname")
and name in ('VISIT', 'VISITNUM')
group by memname
having count(distinct name) = 2; /* Ensure both VISIT and VISITNUM are present */
quit;
/* Step 3: Check if any datasets were found */
%if &sqlobs = 0 %then %do;
%put No datasets in &libname contain both VISIT and VISITNUM variables.;
%end;
%else %do;
%put The following datasets contain both VISIT and VISITNUM variables: &dslist;
/* Initialize an empty dataset for combined VISIT and VISITNUM values */
data combined_visits;
length Dataset $32 VISIT $200 VISITNUM 8;
stop;
run;
/* Step 4: Loop through each dataset */
%let ds_count = %sysfunc(countw(&dslist));
%do i = 1 %to &ds_count;
%let dsname = %scan(&dslist, &i);
/* Extract unique VISIT and VISITNUM values, excluding UNSCHEDULED visits */
proc sql;
create table visit_&dsname as
select distinct "&&dsname" as Dataset, VISIT, VISITNUM
from &libname..&&dsname
where VISIT is not missing and VISITNUM is not missing
and VISIT not like 'UNSCH%'; /* Exclude UNSCHEDULED visits */
quit;
/* Append to the combined dataset */
proc append base=combined_visits data=visit_&dsname force;
run;
%end;
/* Step 5: Compare combined VISIT/VISITNUM with TV domain */
proc sql;
create table visit_comparison as
select a.*, b.Dataset as In_SDTC_Dataset
from tv_visit a
left join combined_visits b
on a.VISIT = b.VISIT and a.VISITNUM = b.VISITNUM
order by VISITNUM, VISIT;
quit;
/* Step 6: Display the comparison results */
proc print data=visit_comparison;
title "Comparison of VISIT/VISITNUM between TV and SDTM Datasets (Excluding Unscheduled Visits)";
run;
%end;
%mend compare_visit;
/* Run the macro by specifying your SDTM library name */
%compare_visit(libname=sdtm);
This macro performs the following steps:
VISIT
and VISITNUM
values from the TV
domain.VISIT
and VISITNUM
variables by querying the metadata table SASHELP.VCOLUMN
.VISIT
and VISITNUM
values and appends them into a consolidated dataset.TVff
domain and displays any discrepancies.This macro is especially useful when checking that all actual visits recorded in the SDTM datasets align with the planned visits documented in the TV
domain. Ensuring consistency between these values is essential for accurate clinical trial reporting and analysis.
%compare_visit(libname=sdtm);
In this example, the macro will search for VISIT
and VISITNUM
variables in the SDTM datasets located in the sdtm
library and compare them with the values in the TV
domain.
By automating the process of extracting and comparing VISIT
and VISITNUM
values, this macro simplifies what could otherwise be a tedious and error-prone task. It ensures that all visit data is consistent and complete, aligning the planned and actual visits in the SDTM datasets.
Feel free to adapt this macro to meet your specific needs in clinical trials data management!
EPOCH
Values in SDTM Datasets using a SAS MacroAuthor: [Sarath]
Date: [05SEP2024]
The EPOCH
variable is essential in many SDTM datasets as it helps describe the period during which an event, observation, or assessment occurs. In clinical trials, correctly capturing and analyzing the EPOCH
variable across datasets is crucial. This post walks through a SAS macro program that automates the process of finding all EPOCH
values from any dataset within an entire library of SDTM datasets.
This macro program loops through all the datasets in a specified library, checks for the presence of the EPOCH
variable, and extracts the unique values of EPOCH
from each dataset. It then consolidates the results and displays them for review.
EPOCH
variable.EPOCH
variable for each dataset.Here’s the macro that performs the task:
%macro find_epoch(libname=);
/* Get a list of all datasets in the library */
proc sql noprint;
select memname
into :dslist separated by ' '
from sashelp.vcolumn
where libname = upcase("&libname")
and name = 'EPOCH';
quit;
/* Check if any dataset contains the EPOCH variable */
%if &sqlobs = 0 %then %do;
%put No datasets in &libname contain the variable EPOCH.;
%end;
%else %do;
%put The following datasets contain the EPOCH variable: &dslist;
/* Loop through each dataset and extract unique EPOCH values */
%let ds_count = %sysfunc(countw(&dslist));
%do i = 1 %to &ds_count;
%let dsname = %scan(&dslist, &i);
/* Extract unique values of EPOCH */
proc sql;
create table epoch_&dsname as
select distinct '&&dsname' as Dataset, EPOCH
from &libname..&&dsname
where EPOCH is not missing;
quit;
%end;
/* Combine the results from all datasets */
data all_epochs;
set epoch_:;
run;
/* Display the results */
proc print data=all_epochs;
title "Unique EPOCH values across datasets in &libname";
run;
%end;
%mend find_epoch;
/* Run the macro by specifying your SDTM library name */
%find_epoch(libname=sdtm);
The macro works by querying the SASHELP.VCOLUMN
metadata table to check for the presence of the EPOCH
variable in any dataset. It loops through the datasets that contain the variable, extracts distinct values, and aggregates the results into a single dataset.
EPOCH
variable.EPOCH
, it extracts unique values.Imagine you have a large collection of SDTM datasets and need to quickly check which datasets contain the EPOCH
variable and what unique values it holds. Running this macro allows you to do this across your entire library with minimal effort.
%find_epoch(libname=sdtm);
In this example, the macro will search for the EPOCH
variable in the SDTM datasets stored in the library named SDTM
. It will then display the unique values of EPOCH
found in those datasets.
This macro simplifies the task of analyzing the EPOCH
variable across multiple datasets in a library, saving time and reducing manual effort. By leveraging the power of PROC SQL
and macros, you can automate this otherwise tedious process.
Feel free to adapt and expand this macro to suit your specific needs! Happy coding!