## How to calculate number of subject when several treatment periods are available

Super Contributor
Posts: 312

# How to calculate number of subject when several treatment periods are available

Dear,

I am using following code to create macro variables for data one.

But my actual data contains three treatments (trt1,trt2,trt3) and I am repeating the code three times to calculate macro variables. Is there any other solution to simplify code for data two; The placebo values should not included in Total.

data one;
input id trt1 \$;
datalines;
1 10
2 10
3 20
4 30
5 20
6 30
7 30
;
proc sql;
create table NS as
select count(distinct id) as NS,trt1
from one
group by trt1;
quit;

data ns1;
set ns;
trt1=strip(trt1);
if trt1='10' then trt='A';
if trt1='20' then trt='B';
if trt1='30' then trt='C';

run;

data ns2;
set ns1 end=done;
Total + ns;
output;
if done;
ns = TOTAL;
trt='D';
trt1='Total';
output;
drop Total;
run;

data _null_;
set tnstfd11;
call symputx(trt,ns);
run;

data two;
input id trt1 \$; trt2 \$ trt3 \$
datalines;
1 10 20 30
2 10 30 20
3 20 10 place
4 30 10 place
5 20 30 place
6 30 20 place
7 30 20 place

8 10 30 20

;
;

Posts: 1,831

## Re: How to calculate number of subject when several treatment periods are available

`data one;input id trt1 \$;datalines;1 102 103 204 305 206 307 30;run;proc format; value \$trtx '10' = 'A' '20' = 'B' '30' = 'C' ; run;proc sql;create table NS asselect count(distinct id) as NS, strip(trt1) as trt1, put(trt1,\$trtx.) as trtfrom onegroup by trt1;quit;data ns2; set ns end=done;Total + ns;output;if done;ns = TOTAL;trt='D';trt1='Total';output;drop Total;run;`

In case you have more values to convert - add those values to the format TRTX.

Having trt2, trt3 - add similar lines as trt1 to the sql step.

Posts: 1,831

## Re: How to calculate number of subject when several treatment periods are available

Having trt2, trt3 etc. - pay attemtion to next code:

``````proc format;
value \$trtx
'10' = 'A'
'20' = 'B'
'30' = 'C'
other = '*'  /* ???? */
; run;

proc sql;
create table NS as
select count(distinct id) as NS,
strip(trt1) as trt1,
put(trt1,\$trtx.) as trt_1,

strip(trt2) as trt2,
put(trt2,\$trtx.) as trt_2,

strip(trt3) as trt3,
put(trt3,\$trtx.) as trt_3
from one
group by trt1;
quit;
``````

in proc format - see other= row for missing predefined values/

I could not understand what macro variables you want to create.

trt is a result of trt1. How to you want to deal with trt2, trt3 ?

data _null_;
set tnstfd11;   /* where from is this dataset ??? */
call symputx(trt,ns);
run;

Super User
Posts: 9,925

## Re: How to calculate number of subject when several treatment periods are available

Your only macro-related statement is in

``````data _null_;
set tnstfd11;
call symputx(trt,ns);
run;``````

which uses a dataset you have not provided any information for.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Discussion stats
• 3 replies
• 244 views
• 2 likes
• 3 in conversation