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

Can someone please help me quote / mask the below so that it works with all options.

Basically, I want to allow the use to filter the dataset using any SAS condition, so they could enter double quotes, single quotes, OR, = etc.

 

Is there any way where there is no need to mask while calling the macro?

 

data testdata;

length account_status $20 marker $10 prod_type 8;

account_status="Current"; prod_type = 1; marker=''; output;

account_status="30days"; prod_type = 2; marker=''; output;

account_status="60days"; prod_type = 2; marker=''; output;

account_status="90days"; prod_type = 3; marker=''; output;

run;

%MACRO mymacro(param1= );

 

data accounts_data ;

set testdata (where = (MISSING(marker)

%if "&param1." NE "" %then %do;

AND (&param1.)

%end;

 

));

run;

%MEND mymacro;

 

 

/* P1 PD Stability Volume - Volume By PD Pillar 1 Bands (Scored Segments Only) */

%mymacro(param1 = (account_status ^= "Current" and prod_type ^= 1)

 

);

%mymacro(param1 = (prod_type ^= 1)

 

);

%mymacro(param1 =

);

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
This will handle most cases:

%if %length(&param1.) %then and (&param1.);

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

What for? If they can write conditions, they can do the rest of the code anyway.

Creating and maintaining such a macro (and the making of the calls) will be more effort than you could possibly save by using it.

Sanjay_M
Obsidian | Level 7

Initially to get this working.

 

Secondly, these will be then used by the development team to validate the outputs, so they may filter as needed. So I need to bring this in a position where this is working and not many changes are required.

Astounding
PROC Star
This will handle most cases:

%if %length(&param1.) %then and (&param1.);
yabwon
Onyx | Level 15

Hi @Sanjay_M ,

 

Read this article by Susan O'Conor: https://stats.idre.ucla.edu/wp-content/uploads/2016/02/bt185.pdf

 

It will explain you everything.

 

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

You appear to be asking two questions. One is how to handle an optional parameter. Second how to make it easier to pass in complex strings.

 

In general I find that %LENGTH() is a good way to test if the parameter is set.  You can "fool" it by use %STR( ) to send in a non-empty string that only contains blanks.  If that is a concern look at this classic paper by Chung and King that tests many different ways of testing if a macro variable is "blank".  https://support.sas.com/resources/papers/proceedings09/022-2009.pdf

 

For passing values for this problem you might be able to use SYSPBUFF. 

 

But perhaps it is just easier to tell the users to add parentheses around the values.  Extra parentheses will not hurt the logic of your generated WHERE clause.  Although they could still break your earlier test for empty values by passing in parentheses with nothing between them.

yabwon
Onyx | Level 15
Very good reading!
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 1225 views
  • 1 like
  • 5 in conversation