i have a dataset and i need to create macro variable for 2 values. trt and freq
trt | freq |
trt1 | 27 |
trt2 | 28 |
can i create macro variable for trt and freq so that i can use in outputs which i need like trt1 vs trt2 and
trt1(N=27) versus trt2 (N=28).
currently i hardcode it in my program treatGrp = "trt1 (N=27) versus trt2 (N=28)"; and esti="Slope: trt1 vs, trt2"
any help or if it is not clear in my explanation
You did not create macro variable TRT1 or TRT2. Your code would have create macro variables named ARMCD1 and ARMCD2.
I'm a little confused. Are you trying to set the macro variable values using the results of proc freq?
yes i want to set values and use them for ex:
in my trt i have values trt1 and trt2 instead of hard coding i want to macrovise it and use and the same with values
For example:
proc sql noprint; select count(distinct SUBJECT) into: TRT1 from ADSL where TRT="TRT A"; select count(distinct SUBJECT) into: TRT2 from ADSL where TRT="TRT B"; quit; proc report...; define trta / "Treatment (N=%trim(&TRT1.))"; ... run;
Thanks, but there is a problem in it because the trt1 and trt2 will be changed later to soem other treatment arm.
That is the reason i am concerned how to make it automatically
There is no "automatic creation of treatment populations" button or code. Each study will be different as described in the protocol, hence you will need to setup each study per the protocol. For instance there may be crossover designs.
Edit, just to add, I assume this is across studies as its not very often that treatment arms change, and if they do then that requires protocol/SAP etc. updates which would include updating the code.
So you have a summary for number of patients by treatment.
data have ;
input trt $ freq ;
cards;
trtA 27
trtB 28
;
And you want to create macro variables TRT1, TRT2, ....
data _null_;
set have ;
call symputx(cats('trt',_n_),catx(' ',trt,cats('(N=',put(freq,comma16.),')')));
run;
Thanks Tom ,
Can you please let me know how to use it in my hardcoded current step
treatGrp = "trt11 (N=27) versus trt2 (N=28)";
label esti="Slope: trt1 vs. trt2" in datastep.
You can expand the macro variables where you need them.
treatGrp = "&trt1 versus &trt2";
You can use macro functions on them if you need.
label
esti="Slope: %scan(&trt1,1,()) vs. %scan(&trt2,1,())"
;
Sorry Tom for not catching you as i am new to these things.
I get warnings if i do and macro is not resolved
WARNING: Apparent symbolic reference TRT1 not resolved.
WARNING: Apparent symbolic reference TRT2 not resolved.
WARNING: Apparent symbolic reference TRT1 not resolved.
WARNING: Apparent symbolic reference TRT1 not resolved.
WARNING: Apparent symbolic reference TRT2 not resolved.
WARNING: Apparent symbolic reference TRT1 not resolved.
WARNING: Apparent symbolic reference TRT2 not resolved.
Is it something to do with the below
call symputx(cats('trt',_n_),catx(' ',trt,cats('(N=',put(freq,comma16.),')')));
as i am not mentioning anything here about trt1 and trt2 which are treatment arms and may change later
To make it easier to code the NAMES of the macro variables shouldn't change. It is the VALUE of the macro variable that will change.
But you can't use the macro variables before you have created them.
So you said you had a dataset with two observations which had treatment label and count. I showed you a method to convert those two observations into two macro variables in the format you requested.
data _null_;
set have ;
call symputx(cats('trt',_n_),catx(' ',trt,cats('(N=',put(freq,comma16.),')')));
run;
You then showed an example of where you wanted to use the macro variables in later code and I showed you how to use the macro variable value directly and also one of many ways that you can use macro functions to operate on the value of the macro variables.
data ...
...
treatGrp = "&trt1 versus &trt2";
label
esti="Slope: %scan(&trt1,1,()) vs. %scan(&trt2,1,())"
;
...
run;
Perhaps it would be easier to create four macro variables?
data _null_;
set have ;
call symputx(cats('trt',_n_),trt);
call symputx(cats('n',_n_),freq);
run;
data ...
...
treatGrp = "&trt1 (N=&n1) versus &trt2 (N=&n2)";
label
esti="Slope: &trt1 vs. &trt2"
;
...
run;
unfortunately trt1 and trt2 are not getting resolved 😞
Do i need to create them as macro variables like
%macro ttd (trt1=,trt2=);
The CALL SYMPUTX() statements will create the macro variables. If they did not get created either you did not run that data step or you ran it on an empty input dataset so that the CALL SYMPUTX() statements never ran.
i ran it and infact N values are getting resolved.
i renamed the variable name to armcd and it has values dummy1 and dummy2
data _null_;
set dummy_freq ;
call symputx(cats('armcd',_n_),armcd);
call symputx(cats('n',_n_),frequency);
run;
and in next datastep i am using
treatGrp = "&trt1 versus &trt2";
label esti="Slope: &trt1 (N=&n1) versus &trt2 (N=&n2)" ;
You did not create macro variable TRT1 or TRT2. Your code would have create macro variables named ARMCD1 and ARMCD2.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.