Hi guys:
I want to use tranwrd function to translate + of a macro variable to " , " , so that I can use in statement afterward.
my code is like:
%do i= 1 to 3;
%if %index(&&_col&i,+)^=0 %then %let tempcol&i=%sysfunc(tranwrd(&&_col&i,+,","));
however sas log gave me a red mark and a note : The meaning of an identifier after a quoted string might change in a future SAS release . Inserting white space between a quoted string and the succeeding identifier is recommended. (Is there a way to solve this ?)
suppose I have a &_col1= 1 + 2 ,
I want tranwrd &_col1 to 1 " , " 2 so that I can use statement :if col in ("&tempcol1") which is equal to if col in ("1","2");
From where do you create the original macro variable? And is the result to be used in a Subsetting IF that can be replaced by a WHERE?
It seems like a mistake to do this with macro variables. There might be many easier ways to do this. Please take a step back and explain the big picture, explain the big problem without reference to SAS code, rather than what you have provided which is a question about code.
@wangmh008 wrote:
Thanks for the quick reply. The goal is to create a macro for frequency table. when we create the CDISC frequency table we want to count the frequency by treatment group, suppose we have treatment group 1 2 3 4 5, sometimes we also want to count for the combination group like 1+2+3+4, so when I wrote my macro I want to use + to indicate that I want to get the information of combination group %freqtable(trtgrp=1 2 3 4 5 1+2+3+4).
Is there a way to solve this problem by using functions like %quote %unquote ?
I have no idea what code inside a macro you would use to make %freqtable(trtgrp=1 2 3 4 5 1+2+3+4) work and produce meaningful results. I'm not even sure I know what the output would be for trtgrp=1 2 3 4 5 1+2+3+4. The advice I always give to people is to write code that works without macro variables and without macros for one iteration, and then it will be easier to turn that into a macro.
So, show us working code without macros and without macro variables for the case where trtgrp=1 2 1+2
@wangmh008 wrote:
Hi Paige thanks for the advice, the below code could recur the error.
%let grp=1+2;
data temp;
input group;
datalines;
1
2
1
3
;
run;
data temp1;
set temp;
%if %index(&grp,+)^=0 %then %do;
%let tgrp=%sysfunc(tranwrd(&grp,+,","));
if group in ("&tgrp");
%end;
run;
I asked to see code that works for this one case without macros and without macro variables. If you can't write code that works without macros and without macro variables first, then it will NEVER work with macros and with macro variables.
And since you claim you want to create frequency tables, this code does not create frequency tables.
We (or at least I) have the problem that you are showing us tiny bits of the problem, and we can't see how they all fit together to get you from some starting point to actual frequency tables.
@wangmh008 wrote:
Thanks for the quick reply. The goal is to create a macro for frequency table. when we create the CDISC frequency table we want to count the frequency by treatment group, suppose we have treatment group 1 2 3 4 5, sometimes we also want to count for the combination group like 1+2+3+4, so when I wrote my macro I want to use + to indicate that I want to get the information of combination group %freqtable(trtgrp=1 2 3 4 5 1+2+3+4).
Is there a way to solve this problem by using functions like %quote %unquote ?
So that type of macro is going to be generating SAS code. So there is no reason to use macro code to manipulate the value of the TRTGRP macro variable.
Write a data step that can manipulate the value of TRTGRP and convert it into what you need.
If just want to convert "1 2 3 4 5 1+2+3+4" into 6 macro variables it would be trivial using actual SAS code instead of macro code. Much easier than trying to deal with spaces and commas in macro code.
data _null_;
string="&trtgrp";
do index=1 to countw(string,' ');
call symputx(cats('trt',index),tranwrd(scan(string,index,' '),'+',','));
end;
run;
Most likely what you need is a multi-level format. So you could probably generate a CNTLIN dataset that you could pass to PROC FORMAT to create the format. Then use the format with one of the summary procedures that supports multi-level formats.
There is only one place in your code where you have a character following a closing quote:
%let tempcol&i=%sysfunc(tranwrd(&&_col&i,+,","));
Try adding a space after the closing quote:
%let tempcol&i=%sysfunc(tranwrd(&&_col&i,+,"," ));
Verify that macro language ignores that blank, and gives you the result you are looking for.
Seems like this code can work
%if %index(&&_col&i,+)^=0 %then %let tempcol&i=%sysfunc(tranwrd(%str(&&_col&i),%str(+),%str( ", " )));
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.