DATA Step, Macro, Functions and more

How to perform check condition on the macro variable

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

How to perform check condition on the macro variable

Hi all,

 

I need some help with my code.

I have a dataset with three variables:

data1:

loan_id var1 var2 var2

111  5.6 2.6 3.6

222  2.4 7.8 1.2

333  1.6 8.3 4.4

 

I have to perform sensitivity testing on these variables, check how my model is performing when one of these variable's value changes i.e either increase by 10% or decrease by 10%. I would be using these new dataset for model testing.

 

eg:

condition1 : when I'm checking for var1 i.e when var1=(var1*0.9) , I want the other variable's values to be same

I want my dataset to be as follows:

loan_id var1 var2 var2

111  5.04 2.6 3.6

222  2.16 7.8 1.2

333  1.44 8.3 4.4

condition2: when I'm checking for var1 i.e when var1=(var1*1.1) , I want the other variable's values to be same

I want my dataset to be as follows:

loan_id var1 var2 var2

111  6.16 2.6 3.6

222  2.64 7.8 1.2

333  1.76 8.3 4.4

 

The issue I am facing is : is there any way to parameterize these conditions.

Suppose if my testname =var1UP10 then var1=(var1*1.1) or if my testname =var1DOWN10 then var1=(var1*0.9)

similarly for other variables. I want to pass the testname as macrovariable and created six different datasets one-by-one.

 

Thanks in advance

 


Accepted Solutions
Solution
‎01-25-2018 10:14 AM
Frequent Contributor
Posts: 91

Re: How to perform check condition on the macro variable

[ Edited ]
Posted in reply to UshaLatha

This may work fine or give you the general idea of how it would work.

%marco sensitivityfactoring( varname, UporDown, libin, datasetin, libout, datasetout);


/*comment explaining the macro program parameters above*/


/*marco (variable name, the words UP or DOWN,

path to library/folder where your data are, datasetin is the name of the dataset,

path to library/folder where your new data will go, datasetin is the name of the new dataset,*/


/*Macro program*/


/* define libname*/

libname datain "&libin."; /* path to library/folder where your data are.*/


/*bring data into work directory*/

data haveoriginal;

set datain..&datasetin.; /* the & and period(s) (.) here are important*/ /* datasetin is the name of the dataset */

run;

%if "&UporDown."="UP" %then %let Sensitivityfactor=1.1;

/* we are moving the variable data up 10%*/

%else %if "&UporDown."="DOWN" %then %let Sensitivityfactor=0.9;

/* we are moving the variable data down 10%*/


/* define libname*/

libname dataout "&libout."; /* path to library/folder where your data are.*/


data daatout.&datasetout.;

set haveoriginal;

/* we know what variable we want to change because it's name is in this macro parameter variable name "varname"*/


&varname.=&varname.*&Sensitivityfactor.;

run;


/* we are done now.*/

%mend; /* we close the macro program. We only need to run the above code once to start per session*/


/* now we call our macro program like so.*/


%sensitivityfactoring(var1, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var1, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);


%marco sensitivityfactoring( varname, UporDown, libin, datasetin, libout, datasetout);

 

/*comment explaining the macro program parameters above*/

 

/*marco (variable name, the words UP or DOWN,

path to library/folder where your data are, datasetin is the name of the dataset,

path to library/folder where your new data will go, datasetin is the name of the new dataset,*/

 

/*Macro program*/

 

/* define libname*/

libname datain "&libin."; /* path to library/folder where your data are.*/

 

/*bring data into work directory*/

data haveoriginal;

set datain..&datasetin.; /* the & and period(s) (.) here are important*/ /* datasetin is the name of the dataset */

run;

%if "&UporDown."="UP" %then %let Sensitivityfactor=1.1;

/* we are moving the variable data up 10%*/

%else %if "&UporDown."="DOWN" %then %let Sensitivityfactor=0.9;

/* we are moving the variable data down 10%*/

 

/* define libname*/

libname dataout "&libout."; /* path to library/folder where your data are.*/

 

data daatout.&datasetout.;

set haveoriginal;

/* we know what variable we want to change because it's name is in this macro parameter variable name "varname"*/

 

&varname.=&varname.*&Sensitivityfactor.;

run;

 

/* we are done now.*/

%mend; /* we close the macro program. We only need to run the above code once to start per session*/

 

/* now we call our macro program like so.*/

 

%sensitivityfactoring(var1, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var1, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

 

 

 

 

 

View solution in original post


All Replies
Solution
‎01-25-2018 10:14 AM
Frequent Contributor
Posts: 91

Re: How to perform check condition on the macro variable

[ Edited ]
Posted in reply to UshaLatha

This may work fine or give you the general idea of how it would work.

%marco sensitivityfactoring( varname, UporDown, libin, datasetin, libout, datasetout);


/*comment explaining the macro program parameters above*/


/*marco (variable name, the words UP or DOWN,

path to library/folder where your data are, datasetin is the name of the dataset,

path to library/folder where your new data will go, datasetin is the name of the new dataset,*/


/*Macro program*/


/* define libname*/

libname datain "&libin."; /* path to library/folder where your data are.*/


/*bring data into work directory*/

data haveoriginal;

set datain..&datasetin.; /* the & and period(s) (.) here are important*/ /* datasetin is the name of the dataset */

run;

%if "&UporDown."="UP" %then %let Sensitivityfactor=1.1;

/* we are moving the variable data up 10%*/

%else %if "&UporDown."="DOWN" %then %let Sensitivityfactor=0.9;

/* we are moving the variable data down 10%*/


/* define libname*/

libname dataout "&libout."; /* path to library/folder where your data are.*/


data daatout.&datasetout.;

set haveoriginal;

/* we know what variable we want to change because it's name is in this macro parameter variable name "varname"*/


&varname.=&varname.*&Sensitivityfactor.;

run;


/* we are done now.*/

%mend; /* we close the macro program. We only need to run the above code once to start per session*/


/* now we call our macro program like so.*/


%sensitivityfactoring(var1, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var1, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);


%marco sensitivityfactoring( varname, UporDown, libin, datasetin, libout, datasetout);

 

/*comment explaining the macro program parameters above*/

 

/*marco (variable name, the words UP or DOWN,

path to library/folder where your data are, datasetin is the name of the dataset,

path to library/folder where your new data will go, datasetin is the name of the new dataset,*/

 

/*Macro program*/

 

/* define libname*/

libname datain "&libin."; /* path to library/folder where your data are.*/

 

/*bring data into work directory*/

data haveoriginal;

set datain..&datasetin.; /* the & and period(s) (.) here are important*/ /* datasetin is the name of the dataset */

run;

%if "&UporDown."="UP" %then %let Sensitivityfactor=1.1;

/* we are moving the variable data up 10%*/

%else %if "&UporDown."="DOWN" %then %let Sensitivityfactor=0.9;

/* we are moving the variable data down 10%*/

 

/* define libname*/

libname dataout "&libout."; /* path to library/folder where your data are.*/

 

data daatout.&datasetout.;

set haveoriginal;

/* we know what variable we want to change because it's name is in this macro parameter variable name "varname"*/

 

&varname.=&varname.*&Sensitivityfactor.;

run;

 

/* we are done now.*/

%mend; /* we close the macro program. We only need to run the above code once to start per session*/

 

/* now we call our macro program like so.*/

 

%sensitivityfactoring(var1, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var1, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var2, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, UP, C:\path location of file, data1, C:\outputpath, newdatasetname);

%sensitivityfactoring(var3, DOWN, C:\path location of file, data1, C:\outputpath, newdatasetname);

 

 

 

 

 

Contributor
Posts: 28

Re: How to perform check condition on the macro variable

Thanks.. this is exactly what I needed..
Super User
Posts: 6,536

Re: How to perform check condition on the macro variable

[ Edited ]
Posted in reply to UshaLatha

You might be making this much more difficult than it needs to be.  For example:

 

data want;

set have;

array drivers {6} var1-var6;

do dr = 1 to 6;

   driver = vname(drivers{dr});

   original_value = drivers{dr};

   do factor = 0.9, 1.1;

      driver{dr} = factor * driver{dr};

      output;

      driver{dr} = original_value;

   end;

end;

run;

 

Instead of creating new variables, create more observations (using the same variable names).  Each observation contains one set of values to test, and includes the variable DRIVER (name of the driver being tested) and FACTOR (0.9 or 1.1).  ORIGINAL_VALUE is optional and you may decide to drop it.  But it might be convenient to keep it around.

 

Just imagine ... you can use the same modeling equation every time because the variable names don't change.  You just have to subset (or possibly use a BY statement) based on DRIVER and FACTOR.

      

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 103 views
  • 4 likes
  • 3 in conversation