**StudySAS Blog: Mastering Clinical Data Management with SAS** 2024-09-05 23:02:00

Dynamic Macro Creation in SAS: Enhancing Automation and Flexibility

Dynamic Macro Creation in SAS: Enhancing Automation and Flexibility

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.

Why Use Dynamic Macros?

  • Automation: Automate repetitive processes by generating macro variables based on dataset values.
  • Flexibility: Dynamic macros adjust based on the changing content of your datasets.
  • Efficient Code: Using dynamic macros reduces redundancy and ensures that your code adapts to different data structures without manual intervention.

Scenario 1: Generating Macros Based on Dataset Variables

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.

Example 1: Creating Macros for Variable Names

/* 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.

Scenario 2: Automating Data Processing Based on Unique Values

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.

Example 2: Creating Macros for Unique Categories

/* 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.

Scenario 3: Dynamically Generating Conditional Code

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.

Example 3: Conditional Code Generation Based on Data Content

/* 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.

Scenario 4: Dynamic Report Generation

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.

Example 4: Dynamic Report Titles

/* 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.

Best Practices for Dynamic Macro Creation

  • Use PROC SQL with INTO Clauses: This is the most efficient way to generate dynamic macro variables from dataset content.
  • Limit the Number of Macros: Ensure that you don’t exceed the macro variable limit by limiting the number of macros generated.
  • Use &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.
  • Avoid Hardcoding: Whenever possible, rely on dynamic macros instead of hardcoding variable names or values to make your code flexible and adaptable.
  • Error Handling: Implement error handling and checks to ensure that dynamic macros are generated correctly without issues during execution.

Conclusion

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.

**StudySAS Blog: Mastering Clinical Data Management with SAS** 2024-09-05 19:08:00

Comparing VISIT and VISITNUM Values Across SDTM Datasets and the TV Domain

Extracting and Comparing Unique VISIT and VISITNUM Values from SDTM Datasets

Author: [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.

Program Overview

The SAS macro program:

  • Extracts unique VISIT and VISITNUM values from all SDTM datasets in the specified library.
  • Compares these values against those recorded in the TV domain.
  • Highlights any discrepancies between the SDTM datasets and the TV domain.

Macro Code

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);

How the Macro Works

This macro performs the following steps:

  1. It first extracts all unique VISIT and VISITNUM values from the TV domain.
  2. It then identifies all datasets in the specified library that contain the VISIT and VISITNUM variables by querying the metadata table SASHELP.VCOLUMN.
  3. For each identified dataset, the macro extracts the distinct VISIT and VISITNUM values and appends them into a consolidated dataset.
  4. Finally, it compares the combined results from the SDTM datasets against the values in the TVff domain and displays any discrepancies.

Use Case

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.

Example of Use:

%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.

Conclusion

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!

**StudySAS Blog: Mastering Clinical Data Management with SAS** 2024-09-05 18:44:00

Finding EPOCH Values in SDTM Datasets using a SAS Macro

Finding EPOCH Values in SDTM Datasets using a SAS Macro

Author: [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.

Program Overview

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.

Key Features:

  • Automatically identifies SDTM datasets containing the EPOCH variable.
  • Extracts unique values from the EPOCH variable for each dataset.
  • Combines results into a single dataset for ease of review.

Macro Code

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);

How the Macro Works

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.

Steps:

  1. Identifies all datasets in the specified library.
  2. Checks each dataset for the EPOCH variable.
  3. For datasets containing EPOCH, it extracts unique values.
  4. Combines the unique values from all datasets into one result dataset.

Use Case

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.

Example of Use:

%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.

Conclusion

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!