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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
ptimusk
Obsidian | Level 7

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

3 REPLIES 3
ptimusk
Obsidian | Level 7

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);

 

 

 

 

 

UshaLatha
Obsidian | Level 7
Thanks.. this is exactly what I needed..
Astounding
PROC Star

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.

      

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 791 views
  • 4 likes
  • 3 in conversation