Desktop productivity for business analysts and programmers

Generate Dynamic Code

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Generate Dynamic Code

[ Edited ]

Hi,

     

   I have data set and i want to generate dynamic code is it possible  to generate dynamic code from existing sas data sets.

 

This is the datatsets .

DATA rule;
     INFILE DATALINES DELIMITER=',' DSD;
     INPUT val1 $ val2 $ val3 $ val4 $;
DATALINES;
1,,3,4
68,89,80,,
87,,,12
RUN;

 

rule.PNG

 

       i want to generate dynamic code like multiple if condition is given below for above data set. is it possible to generate if data is very large and have many different number of combinations.

 

 
if val1='1' and val2='' and val3='3' val4='4' then
  do;

  end;

 

if val1='68' and val2='89' and val3='80' val4='' then
  do;

  end;

 

if val1='87' and val2='' and val3='' val4='12' then
do;

end;

 

 


Accepted Solutions
Solution
‎02-07-2018 03:43 AM
Super User
Posts: 6,932

Re: Generate Dynamic Code

Posted in reply to ramanandyadav

I am interpreting the question you are asking a little differently.  I think you want to make the "generated code" available at a later date.  For example, here is how the program could construct a separate file holding the generated code.

 

data _null_;

set rule;

file 'path to some file of your choice' noprint;

put "if val1='" val1 +(-1) "' and val2='" val2 +(-1)

    "' and val3='" val3 +(-1) "' and val4='" val4 +(-1) "' then";

put "   do;" 

  / "   end;"

  / ;

run;

 

As with any long series of IF/THEN statements, consider whether it would be wise to add the word ELSE.

 

When this DATA step is over, the file that you have specified in quotes in the FILE statement will hold the code you are asking for.  You might edit that code to fill in what needs to be done, before %INCLUDE brings it in.

View solution in original post


All Replies
Super User
Posts: 10,568

Re: Generate Dynamic Code

Posted in reply to ramanandyadav

Follow the basic macro development scheme:

  • write s single instance of code, and make it work
  • identify parts that need to change
  • replace by macro variables, set the macrovars, test again
  • wrap into a macro definition, supply the macrovars as parameters, test again
  • add macro code for more complex code generation

Once such a macro works, you can create a control dataset that contains your series of parameters, and use call execute off that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎02-07-2018 03:43 AM
Super User
Posts: 6,932

Re: Generate Dynamic Code

Posted in reply to ramanandyadav

I am interpreting the question you are asking a little differently.  I think you want to make the "generated code" available at a later date.  For example, here is how the program could construct a separate file holding the generated code.

 

data _null_;

set rule;

file 'path to some file of your choice' noprint;

put "if val1='" val1 +(-1) "' and val2='" val2 +(-1)

    "' and val3='" val3 +(-1) "' and val4='" val4 +(-1) "' then";

put "   do;" 

  / "   end;"

  / ;

run;

 

As with any long series of IF/THEN statements, consider whether it would be wise to add the word ELSE.

 

When this DATA step is over, the file that you have specified in quotes in the FILE statement will hold the code you are asking for.  You might edit that code to fill in what needs to be done, before %INCLUDE brings it in.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 201 views
  • 1 like
  • 3 in conversation