DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

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

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


Accepted Solutions
Solution
‎04-18-2017 09:50 AM
Super User
Super User
Posts: 7,046

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to alexdsa310

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


All Replies
Occasional Contributor
Posts: 5

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to alexdsa310

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

Contributor
Posts: 41

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to jessicaking

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

Super User
Super User
Posts: 7,955

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to alexdsa310

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;
Contributor
Posts: 41

Re: i have a dataset and i need to create macro variable for 2 values

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

Super User
Super User
Posts: 7,955

Re: i have a dataset and i need to create macro variable for 2 values

[ Edited ]
Posted in reply to alexdsa310

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.

 

Super User
Super User
Posts: 7,046

Re: i have a dataset and i need to create macro variable for 2 values

[ Edited ]
Posted in reply to alexdsa310

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;
Contributor
Posts: 41

Re: i have a dataset and i need to create macro variable for 2 values

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.

 

Super User
Super User
Posts: 7,046

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to alexdsa310

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,())"
;
Contributor
Posts: 41

Re: i have a dataset and i need to create macro variable for 2 values

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

Super User
Super User
Posts: 7,046

Re: i have a dataset and i need to create macro variable for 2 values

[ Edited ]
Posted in reply to alexdsa310

 

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;
Contributor
Posts: 41

Re: i have a dataset and i need to create macro variable for 2 values

unfortunately trt1 and trt2 are not getting resolved :-(

Do i need to create them as macro variables like

%macro ttd (trt1=,trt2=);

 

Super User
Super User
Posts: 7,046

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to alexdsa310

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.

Contributor
Posts: 41

Re: i have a dataset and i need to create macro variable for 2 values

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

Solution
‎04-18-2017 09:50 AM
Super User
Super User
Posts: 7,046

Re: i have a dataset and i need to create macro variable for 2 values

Posted in reply to alexdsa310

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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