Sensitivity analysis for different values of few variables; %let

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Sensitivity analysis for different values of few variables; %let

Good morning,

I have a program and I want to run this program with different values of few parameters for sensitivity analysis.

Currently my whole code look like that:

%let parameter1=8;

%let parameter2=3;

*-------------- body of the code----------------;

...

... variable1= &parameter1;

... variable2= &parameter2;

%Macro abc( a=, b=, c=);

...

%Macro xyz (x=, y=,z=);

...

export to excel

*------------end of code-----------------------;

So now, each time it finish 1 check, I have to go back to the code and change the value in the %let section

%let parameter1=8;

%let parameter2=3;

Since the body already has a number of macro, I rather not make the body more complicate and I would like to know if there is better way to structure the first section of %let so that SAS can do the change value by itself. Yes, I can do minor change in the body of the code but I rather not to do much change.

I imagine that I can list the value of parameter1 and parameter2 and SAS do the combination of the value. Something like a

Do parameter1=1 to 10;

Do parameter2=1to 20;

Of course it is the best I can imagine.

If not, I don't mind if I have to list all combination I want to test if I need to do so. For example ( parameter1=8,  parameter2=3);  ( parameter1=4,  parameter2=10)

If any of that can be done, it will help my work greatly.

I appreciate your help.

HHC


Accepted Solutions
Solution
‎10-15-2013 01:31 PM
Super User
Super User
Posts: 6,500

Re: Sensitivity analysis for different values of few variables; %let

You need to pick whether you are using filerefs or physical paths.

Filerefs:

FILENAME W3 TEMP; --> FILE W3; --> %INCLUDE W3;

Physical paths;

FILE "w3.sas"; --> %INCLUDE "w3.sas";

SAS Filerefs (defined by a FILENAME statement) allow you to use short (8 characters or less) aliases for your actual files.  When you use them in SAS statements like %INC or FILE you do NOT put the name in quotes.  SAS will use the physical name defined for that fileref.

You also stumbled onto another "feature" of SAS. If you use an unquoted name in the FILE or INFILE statement that DOES NOT match a fileref then SAS assumes you want to use that name as the physical name with a ".dat" extension appended to it.  BUT when you do that in a %INCLUDE statement SAS will assume you want a program and so appends a ".sas" extension.

View solution in original post


All Replies
Regular Contributor
Posts: 244

Re: Sensitivity analysis for different values of few variables; %let

%macro yourcode(parameter1=,parameter2=);

< your code >

%mend yourcode;

%yourcode(parameter1=8,parameter2=3);

%yourcode(parameter1=4,parameter2=5);

etc.

You can generate those macro calls from a dataset, if your p1/p2 are in a dataset. 

proc sql;

select cats('%yourcode(parameter1=',p1,',parameter2=',p2,')') into :ycalllist separated by ' '

from yourdataset;

quit;

&ycalllist.

Super User
Super User
Posts: 6,500

Re: Sensitivity analysis for different values of few variables; %let

One way is to build a macro from your existing code.

%macro new (parameter1,parameter2);

*-------------- body of the code----------------;

...

... variable1= &parameter1;

... variable2= &parameter2;

%Macro abc( a=, b=, c=);

...

%Macro xyz (x=, y=,z=);

...

export to excel

*------------end of code-----------------------;

%mend new;

Then to call it multiple times that are a number of ways.

One is to use a data step and CALL EXECUTE.

data _null_;

  do parameter1=1 to 10;

    do parameter2=1 to 20;

      call execute(cats('%nrstr(%new)(parameter1=,' , parameter1, ',parameter2=',parameter2,')'));

    end;

  end;

run;


Another is to generate the code using PUT statements and then %INCLUDE it.

filename code temp;

data _null_;

  do parameter1=1 to 10;

    do parameter2=1 to 20;

      put '%new(' parameter1= ',' parameter2= ')' ;

    end;

  end;

run;

%include code / source2 ;


Or you could do it withOUT creating a new macro. Instead just place your original code into a file by itself that then just %INC it where you want it.

filename code temp;

data _null_;

  do parameter1=1 to 10;

    do parameter2=1 to 20;

      put '%let ' parameter1= ';'

        / '%let ' parameter2= ';'

         / '%include "original_program.sas" ;'

      ;

    end;

  end;

run;

%include code / source2 ;


Super Contributor
Posts: 371

Re: Sensitivity analysis for different values of few variables; %let

Thank you so much for your valuable post, Tom.

As always, my learning curve rocket!

I like your 3rd method, since it is quite close to what I imagine.

 

Here is how I did it and my problems:

filename w3 temp;

data _null_;
  do pip_tiny=10 to 11;
    do distance02=8 to 9;
      put '%let ' pip_tiny= ';'
         /  '%let ' distance02= ';'
         / '%include "C:\Users\Hoang\Documents\My SAS Files\9.3\test\w3.sas" ;'
      ;
    end;
  end;
run;
%include w3 / source2 ;

First, the original code is called and run. However it seems that the macro variable is not included and it cause the below error message.

938  +data originaldata; set originaldata;
939  +if body< &pip_tiny/10000 then tooclose=1; else tooclose=0;run;
               -
               22
WARNING: Apparent symbolic reference PIP_TINY not resolved.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,
              a numeric constant, a datetime constant, a missing value, bitstring, INPUT,
              PUT.

Second, the running of original code is stop at level 1 with this message:

NOTE: %INCLUDE (level 1) ending.

I am not sure what I did wrong since your instruction is very clear.

Thank you so much.

HHC

Super User
Super User
Posts: 6,500

Re: Sensitivity analysis for different values of few variables; %let

You left out the FILE statement in the DATA _NULL_ step (check if I left it out in the example) so that the generated code is written to the LOG instead of the file W3 that you are trying to create.

filename w3 temp;

data _null_;

  file w3 ;
  do pip_tiny=10 to 11;

...

Super Contributor
Posts: 371

Re: Sensitivity analysis for different values of few variables; %let

Thank you, Tom.

I add the "file w3" in, however, somehow it still does not make any changes to the error messages .

I don't know if it helps but below is the log when I run  the section you suggested.

HHC

6203  filename w3 temp;
6204
6205  data _null_;
6206      file w3;
6207    do pip_tiny=10 to 11;
6208      do distance02=8 to 9;
6209        put  '%let ' pip_tiny= ';'
6210            / '%let ' distance02= ';'
6211           / '%include "C:\Users\Documents\My SAS Files\.3\w3.sas" ;'
6212        ;
6213      end;
6214    end;
6215  run;

NOTE: The file W3 is:
      Filename=C:\Users\AppData\Local\Temp\SAS Temporary Files\_TD5420_HP_NEW_\#LN00062,
      RECFM=V,LRECL=256,File Size (bytes)=0,
      Last Modified=15Oct2013:12:35:57,
      Create Time=15Oct2013:12:35:57

NOTE: 16 records were written to the file W3.
      The minimum record length was 0.
      The maximum record length was 74.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


6216  %include 'C:\Users\Documents\My SAS Files\w3.sas' / source2 ;
NOTE: %INCLUDE (level 1) file C:\Users\Documents\My SAS Files\w3.sas is
      file C:\Users\Documents\My SAS Files\w3.sas.

Super User
Super User
Posts: 6,500

Re: Sensitivity analysis for different values of few variables; %let

Why are you using a different file in the %INCLUDE than you generated in the data step?

6203  filename w3 temp;

6204

6205  data _null_;

6206      file w3;

....

NOTE: The file W3 is:

      Filename=C:\Users\AppData\Local\Temp\SAS Temporary Files\_TD5420_HP_NEW_\#LN00062,

....

6216  %include 'C:\Users\Documents\My SAS Files\w3.sas' / source2 ;

Super Contributor
Posts: 371

Re: Sensitivity analysis for different values of few variables; %let

I really don't know. I guess what happened is that: Windows7 store Temporary Files under the Temp folder when I use the "filename w3 temp;"

So I delete this "filename w3 temp;" and it look like that. I am not sure why it show up as it is.

1
2    data _null_;
3        file w3;
4      do pip_tiny=10 to 11;
5        do distance02=8 to 9;
6          put  '%let ' pip_tiny= ';'
7               '%let ' distance02= ';'
8              '%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;'
9          ;
10       end;
11     end;
12   run;

NOTE: The file W3 is:
      Filename=C:\Users\Hoang\w3.dat,
      RECFM=V,LRECL=256,File Size (bytes)=0,
      Last Modified=15Oct2013:13:02:13,
      Create Time=15Oct2013:13:02:13

NOTE: 4 records were written to the file W3.
      The minimum record length was 111.
      The maximum record length was 111.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds


13   %include 'C:\Users\Hoang\Documents\My SAS Files\w3.sas' / source2 ;
NOTE: %INCLUDE (level 1) file C:\Users\Hoang\Documents\My SAS Files\w3.sas is
      file C:\Users\Hoang\Documents\My SAS Files\w3.sas.

------------------------------------

Also, when I try NOT include the "file w3;" as below, SAS seems to write the value to the file but still the problem is not resolved.

data _null_;
  do pip_tiny=10 to 11;
    do distance02=8 to 9;
      put  '%let  ' pip_tiny= ';'
          / '%let ' distance02= ';'
          / '%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;'
      ;
    end;
  end;
run;
%include 'C:\Users\Hoang\Documents\My SAS Files\w3.sas' / source2 ;


4400
4401  data _null_;
4402    do pip_tiny=10 to 11;
4403      do distance02=8 to 9;
4404        put  '%let  ' pip_tiny= ';'
4405            / '%let ' distance02= ';'
4406            / '%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;'
4407        ;
4408      end;
4409    end;
4410  run;

%let  pip_tiny=10 ;
%let distance02=8 ;
%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;
%let  pip_tiny=10 ;
%let distance02=9 ;
%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;
%let  pip_tiny=11 ;
%let distance02=8 ;
%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;
%let  pip_tiny=11 ;
%let distance02=9 ;
%include "C:\Users\Hoang\Documents\My SAS Files\w3.sas" ;
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

Solution
‎10-15-2013 01:31 PM
Super User
Super User
Posts: 6,500

Re: Sensitivity analysis for different values of few variables; %let

You need to pick whether you are using filerefs or physical paths.

Filerefs:

FILENAME W3 TEMP; --> FILE W3; --> %INCLUDE W3;

Physical paths;

FILE "w3.sas"; --> %INCLUDE "w3.sas";

SAS Filerefs (defined by a FILENAME statement) allow you to use short (8 characters or less) aliases for your actual files.  When you use them in SAS statements like %INC or FILE you do NOT put the name in quotes.  SAS will use the physical name defined for that fileref.

You also stumbled onto another "feature" of SAS. If you use an unquoted name in the FILE or INFILE statement that DOES NOT match a fileref then SAS assumes you want to use that name as the physical name with a ".dat" extension appended to it.  BUT when you do that in a %INCLUDE statement SAS will assume you want a program and so appends a ".sas" extension.

Super Contributor
Posts: 371

Re: Sensitivity analysis for different values of few variables; %let

OMG, TOM!!!!

I got it now with your latest post!!!

Thank you.

HHC

filename w3 temp;

data _null_;
file w3;
  do pip_tiny=10 to 11;
    do distance02=8 to 9;
      put  '%let  ' pip_tiny= ';'
          / '%let ' distance02= ';'
         / '%include "C:\Users\Hoang\Documents\My SAS Files\9.3\SAS Forex\D1\w3.sas" ;'
      ;
    end;
  end;
run;
%include w3 / source2 ;

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 374 views
  • 6 likes
  • 3 in conversation