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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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

5 REPLIES 5
ChrisBrooks
Ammonite | Level 13

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

Alkibiades
Obsidian | Level 7

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.

ChrisBrooks
Ammonite | Level 13

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.

Alkibiades
Obsidian | Level 7

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.

ChrisBrooks
Ammonite | Level 13

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 5 replies
  • 1047 views
  • 0 likes
  • 2 in conversation