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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Patrick_0-1626295370015.png

 

View solution in original post

12 REPLIES 12
Tom
Super User Tom
Super User

Can you explain what you are trying to do?

Show the SAS code you are trying to use the macro code to generate.

tarheel13
Rhodochrosite | Level 12

Did you try wrapping it in %str in the %let statement? 

 

Example: 

%let STP_TEST1= %str(Mean - 1DP - 50 %);
Patrick
Opal | Level 21

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;

Patrick_0-1626295370015.png

 

BBP
Fluorite | Level 6 BBP
Fluorite | Level 6

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:

BBP_0-1626342091630.png

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

BBP
Fluorite | Level 6 BBP
Fluorite | Level 6
Hi Patrick,
Please ignore above request, the error is because of double quotes.
Thanks,
Bhanu
BBP
Fluorite | Level 6 BBP
Fluorite | Level 6

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

Tom
Super User Tom
Super User

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%*;
Tom
Super User Tom
Super User

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;

 

BBP
Fluorite | Level 6 BBP
Fluorite | Level 6
Thanks Tom
BBP
Fluorite | Level 6 BBP
Fluorite | Level 6

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

Tom
Super User Tom
Super User

@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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 1255 views
  • 2 likes
  • 4 in conversation