Published on :
Macro CREATION_INTERNE

Counting Subjects by Treatment Group

This code is also available in: Deutsch Español Français
Awaiting validation
The script creates a demo dataset `sample_adsl` containing information on subjects and their treatment groups (`trta`, `trtan`). A macro `%count` is then defined, which takes as parameters the input dataset, variables to consider for sorting and counting, and the numerical range of treatment groups. The macro uses `PROC SQL` to count distinct subjects (`usubjid`) for each treatment group within the specified range and stores these counts in global macro variables (e.g., `bign0`, `bign1`, etc.). Finally, the macro is called with the appropriate parameters for the `sample_adsl` dataset and treatment groups 0 to 3.
Data Analysis

Type : CREATION_INTERNE


The `sample_adsl` dataset is created directly within the script using a DATA step. It does not depend on unmanaged external data sources or SASHELP libraries.

1 Code Block
DATA STEP Data
Explanation :
This DATA step creates the `sample_adsl` dataset. It is populated with five observations, each representing a subject (`usubjid`) assigned to a treatment group (`trta` with its numerical version `trtan`). This dataset serves as a test basis for subject counting.
Copied!
1DATA sample_adsl;
2 trta="Placebo";trtan=0;usubjid="A1";OUTPUT;
3 trta="Drug A";trtan=1;usubjid="B2";OUTPUT;
4 trta="Drug B";trtan=2;usubjid="C3";OUTPUT;
5 trta="Placebo";trtan=0;usubjid="D4";OUTPUT;
6 trta="Drug A";trtan=1;usubjid="E5";OUTPUT;
7RUN;
2 Code Block
MACRO %COUNT
Explanation :
This macro named `%count` is designed to count distinct subjects. It supports an input dataset (`data`), counting and sorting variables (`var`), and a range of treatment groups (`trts` to `trte`). Inside the macro, `PROC SQL` is used to create a temporary table `count` and then, through a `%do` loop, iterate over each treatment group. For each group, it counts unique subjects (`usubjid`) and stores the result in a dynamic global macro variable (e.g., `bign0`, `bign1`). The results are displayed in the SAS log via `%put`.
Copied!
1%macro count(DATA=,var=,trts=,trte=);
2PROC SQL noprint;
3 create TABLE count as select &var. from &DATA. order BY &var.;
4 %DO i=&trts. %to &trte.;
5 %global bign&i.;
6 select count(distinct usubjid) into: bign&i. from count where trtan=&i;
7 %put bign&i. &&bign&i.;
8 %END;
9QUIT;
10%mend count;
3 Code Block
MACRO %COUNT CALL
Explanation :
This call executes the `%count` macro. It provides the previously created `sample_adsl` dataset, specifies the variables `trtan`, `trta`, and `usubjid` for the operation, and requests counting for treatment groups ranging from 0 to 3. This will generate global macro variables such as `bign0`, `bign1`, `bign2`, `bign3` containing the subject counts for each respective group.
Copied!
1%count(
2DATA=sample_adsl,var=%str(trtan,trta,usubjid),trts=0,trte=3);
3 
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Program Name : Bign_counter.sas * Author : Balaji.M * SAS Version : SAS 9.4 or higher * Description : This SAS program is use to generate subject counts from adsl dataset with dummy treatment groups in below example Drug C is counted with dummy values for testing purpose. *Macro variable : data:- Input Dataset var:- Variables used for counting and sorting purpose should be included trts:- treatment group start numeric value trte:- treatment group end numeric value