The subject may be a little confusing but here is what I want to achive with DS2:
I have a dataset containing a field which contains values I want to use as code, when invoking a method in ds2.
In case of a macro I would use something like that:
%macro subu(ruu1);
data test;
set sas_data.orders;
if &ruu1. then output;
run;
%mend subu;
data _null_;
set testdat;
call execute('%subu('||vari||')');
run;
In this case dataset testdat only having one obs and one variable vari which contains the value: ordertype eq 3
Is there an option in ds2 to call a method with field values of a dataset as parameters which could be used as code it executes in said method?
Or can macros be called from a ds2 anyhow?
I thought that would be the case - I've seen systems like that before and they work well as long as the user understands the syntax for the rules and the rules are fairly simple. I think my first suggestion is probably your optimal solution.
The simplest way might be to load your rules into macro variables before your Proc DS2 step and then using them as in this example
data rules;
length rulename $6 ruleval $20;
infile datalines dlm=",";
input rulename ruleval;
datalines;
rule1,age>12
rule2,height>60
;
run;
data class;
set sashelp.class;
run;
/* Load rules into macro variables */
data _null_;
set rules;
call symput(rulename,ruleval);
run;
/* DS2 Program */
proc ds2;
data out(overwrite=yes);
method run();
set class;
if &rule1 and &rule2 then output;
end;
method term();
end;
enddata;
run; quit;
I have done similar things in old school "classic" data step and as I'm sure you're aware it can become very complicated very quickly...
An exellent suggestion, thanks.
The reason why I so desperately search for a method to incorporate macro functionality in ds2 programming is that I am the first in my department starting to deal with ds2 and I would love to have good arguments praising its versatility to make it popular amongst my collagues.
You can create DS2 packages which are in effect method libraries and which would replace traditional macros. That, I think, might be the best way forward rather than trying to shoehorn macros into a scenario they weren't really designed for.
Another option might be to create FCMP functions which can themselves call macros but the calling syntax is somewhat different for those macros.
I already considered FCMP functions but wanted to find a way especially in ds2 because there are plenty of validy checks for data I could very nicely do with methods. The challenge to me is how to use field values from datasets (I created from importing data from Excel control sheets) as code itself.
The user maintaining the mentioned control sheets so to say enters rules in fields that are to be applied as code to validate data by my program.
I thought that would be the case - I've seen systems like that before and they work well as long as the user understands the syntax for the rules and the rules are fairly simple. I think my first suggestion is probably your optimal solution.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: