BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alexdsa310
Obsidian | Level 7

i have a dataset and i need to create macro variable for 2 values. trt and freq

trtfreq
trt127
trt228

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You did not create macro variable TRT1 or TRT2. Your code would have create macro variables named ARMCD1 and ARMCD2.

View solution in original post

14 REPLIES 14
jessicaking
SAS Employee

I'm a little confused. Are you trying to set the macro variable values using the results of proc freq?

alexdsa310
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
alexdsa310
Obsidian | Level 7

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

Tom
Super User Tom
Super User

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;
alexdsa310
Obsidian | Level 7

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.

 

Tom
Super User Tom
Super User

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,())"
;
alexdsa310
Obsidian | Level 7

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

Tom
Super User Tom
Super User

 

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;
alexdsa310
Obsidian | Level 7

unfortunately trt1 and trt2 are not getting resolved 😞

Do i need to create them as macro variables like

%macro ttd (trt1=,trt2=);

 

Tom
Super User Tom
Super User

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.

alexdsa310
Obsidian | Level 7

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

Tom
Super User Tom
Super User

You did not create macro variable TRT1 or TRT2. Your code would have create macro variables named ARMCD1 and ARMCD2.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 14 replies
  • 1533 views
  • 1 like
  • 4 in conversation