DATA Step, Macro, Functions and more

DS2 and usage of variable values in methods as code similar to macros

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

DS2 and usage of variable values in methods as code similar to macros

[ Edited ]

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?


Accepted Solutions
Solution
2 weeks ago
Valued Guide
Posts: 593

Re: DS2 and usage of variable values in methods as code similar to macros

Posted in reply to Alkibiades

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.

View solution in original post


All Replies
Valued Guide
Posts: 593

Re: DS2 and usage of variable values in methods as code similar to macros

Posted in reply to Alkibiades

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...

Occasional Contributor
Posts: 12

Re: DS2 and usage of variable values in methods as code similar to macros

Posted in reply to ChrisBrooks

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.

Valued Guide
Posts: 593

Re: DS2 and usage of variable values in methods as code similar to macros

Posted in reply to Alkibiades

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.

Occasional Contributor
Posts: 12

Re: DS2 and usage of variable values in methods as code similar to macros

[ Edited ]
Posted in reply to ChrisBrooks

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.

Solution
2 weeks ago
Valued Guide
Posts: 593

Re: DS2 and usage of variable values in methods as code similar to macros

Posted in reply to Alkibiades

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 91 views
  • 0 likes
  • 2 in conversation