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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

2 REPLIES 2
Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 1413 views
  • 1 like
  • 3 in conversation