Hi
Please help resolving the below code:
%let STP_TEST1= Mean - 1DP - 50 %;
%let STP_TEST2= Minimum- 1DP - 50 %;
%let STP_TEST3= Maximum - 1DP - 50 %;
%let STP_TEST_COUNT=3;
%macro build_filter_display(varname);
(%if &&&varname._Count. eq 1 %then %trim(&&&varname); %else %do i=1 %to &&&varname._Count.; %if &i. NE 1 %then,%str( ); %trim(&&&varname&i.)%end;)
%mend;
%macro selection_data;
data test;
length Selection $4000.;
label Selection="Selection Criteria";
%if (&STP_TEST_COUNT. GT 0) %then
Selection="Test in %build_filter_display(STP_TEST)" %str(;) Output%str(;);
run;
%mend;
%selection_data;
above code is outputting as
Test in (Mean - 1DP - 50 %2 NE 1 , Minimum- 1DP - 50 %3 NE 1 , Maximum - 1DP - 50 %)
I want it as
Test in (Mean - 1DP - 50 % , Minimum- 1DP - 50 % , Maximum - 1DP - 50 %)
Thanks,
Bhanu
Below macro returns what you're asking for.
%let STP_text1= Mean - 1DP - 50 %;
%let STP_text2= Minimum- 1DP - 50 %;
%let STP_text3= Maximum - 1DP - 50 %;
%let STP_text_COUNT=3;
%macro build_filter_display(varname);
%local text;
%do i=1 %to &&&varname._COUNT;
%if &i= 1 %then
%let text=%nrstr(&&&varname).&i;
%else
%let text=&text, %nrstr(&&&varname).&i;
%end;
%unquote(&text)
%mend;
data text;
length Selection $4000.;
label Selection="Selection Criteria";
if &STP_text_COUNT > 0 then
do;
Selection="text in (%build_filter_display(STP_text))";
output;
end;
stop;
run;
Can you explain what you are trying to do?
Show the SAS code you are trying to use the macro code to generate.
Did you try wrapping it in %str in the %let statement?
Example:
%let STP_TEST1= %str(Mean - 1DP - 50 %);
Below macro returns what you're asking for.
%let STP_text1= Mean - 1DP - 50 %;
%let STP_text2= Minimum- 1DP - 50 %;
%let STP_text3= Maximum - 1DP - 50 %;
%let STP_text_COUNT=3;
%macro build_filter_display(varname);
%local text;
%do i=1 %to &&&varname._COUNT;
%if &i= 1 %then
%let text=%nrstr(&&&varname).&i;
%else
%let text=&text, %nrstr(&&&varname).&i;
%end;
%unquote(&text)
%mend;
data text;
length Selection $4000.;
label Selection="Selection Criteria";
if &STP_text_COUNT > 0 then
do;
Selection="text in (%build_filter_display(STP_text))";
output;
end;
stop;
run;
Thanks Patrick, your code resolved bit of my requirement, now I need the values in quotes, for that I have used following code:
%macro build_filter_display(varname);
(
%let test=;
%do i = 1 %to &&&varname._Count.;
%if &i = 1 %then
%if %nrbquote(%trim(&&&varname)) eq %nrbquote() %then '-1';
%else %let test=%trim(%nrstr(%")%nrstr(%trim(&&&varname)).%nrstr(%"));
%else
%if %nrbquote(%trim(&&&varname&i.)) eq %nrbquote() %then '-1';
%else
%let test=&test,%str( )%nrstr(%")%trim(%nrstr(&&&varname).&i)%nrstr(%");
%end;
%unquote(&test)
)
%mend;
/*%build_in_statement_oracle2;*/
%put Test in %build_in_statement_oracle(STP_TEST);
which is generating outcome as:
may be that highlighted bit causing issue in my oracle query
MPRINT(BUILD_FILTER_DISPLAY): (
MPRINT(BUILD_FILTER_DISPLAY): "BA - Label Claim - Mean - 1DP - 50 %","BA - Label Claim - Minimum- 1DP - 50 %","BA - Label Claim - Maximum - 1DP - 50 %" )
ERROR: ORA-00972: identifier is too long.
any help is appreciated.
Thanks
Hi Patrick,
AS I mentioned the gap before the values is returning no data in further steps. is there any way to remove spaces before the values?
Thanks,
Bhanu
When emitting a series of text in a macro to be used as a string you can make sure that no separating spaces are generated by not including white space around the text to be emit. For example by adding some macro comments.
%*;&mvar%*;
Either spend some time adding macro quoting in your macro to protect the generated text:
%macro build_filter_display(varname);
%local i mvar sep;
(%do i=1 %to &&&varname._count;
%let mvar=&varname.&i;&sep.%superq(&mvar)%let sep=%str( , );
%end;)
%mend build_filter_display;
%put "Test in %build_filter_display(stp_test)" ;
Or since you use the value in SAS code use regular SAS code instead of macro code to generate the value you want from that list of macro variables.
%let basename=STP_TEST;
data test;
length Selection $4000.;
label Selection="Selection Criteria";
do i=1 to symgetn("&basename._count");
selection=catx(' , ',selection,symget(cats("&basename",i)));
end;
selection=cats('Test in (',selection,')');
put selection= $quote.;
run;
Hi Tom,
your macro works but sometimes when COUNT = 1 then we have value with no suffix. for example
%let STUDYID=S_12345;
%let STUDYID_COUNT=1;
in this case your macro is not working, I prefer macro than data step, any help appreciated.
Regards,
Bhanu
@BBP wrote:
Hi Tom,
your macro works but sometimes when COUNT = 1 then we have value with no suffix. for example
%let STUDYID=S_12345;
%let STUDYID_COUNT=1;
in this case your macro is not working, I prefer macro than data step, any help appreciated.
Regards,
Bhanu
I have seen that inconsistent behavior mentioned before. Here is modification to fix it.
Basically just make sure that the suffix 1 variable exists by copying the value of the variable without a suffix.
%macro build_filter_display(varname);
%local i mvar sep;
%if &&&varname._count = 1 %then %let &varname.1=%superq(&varname);
(%do i=1 %to &&&varname._count;
%let mvar=&varname.&i;&sep.%superq(&mvar)%let sep=%str( , );
%end;)
%mend build_filter_display;
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.