title: Conditiona a SAS Macro base on names in a list
body: <p>new here.</p>
  
  <p>I have written a pair of macros to search through one of our databases at work. This is an exemple of how I used it:</p>
  
  <pre><code>%LET SVlist=%str(R_PAR_SV_2012_VER3 R_PAR_SV);
  %LET ovlist=%str(R_PAR_OV_2012_VER3 R_PAR_OV);
  
  
  
  
  %LET FILTER=
  (DIAGNOS like 'S72%' or diagnos contains ' S72' or
  DIAGNOS like 'I21%' or diagnos contains ' I21'
  );
  
  %LET OVSELECT=PNR,
            PNRQ, 
            INDATUM, 
            OP, 
            DIAGNOS, 
            HDIA,
            alder,
            SJUKHUS;
  %LET sVSELECT=&amp;OVSELECT,UTDATUM,OPD1;
  
  %macro ovsql(ovlist);
  proc sql noprint;
  %let n=%sysfunc(countw(&amp;ovlist));
      %do i=1 %to &amp;n;
          %let val = %scan(&amp;ovlist,&amp;i);
  
  
          create table work.&amp;val as
  
              select &amp;ovselect,
  'SV' as souce
  from r_par.&amp;val
              where pnrq='0';
  
  
      %end;
  quit;
  %mend;
  
  %macro svsql(svlist);
  proc sql noprint;
  %let n=%sysfunc(countw(&amp;svlist));
      %do i=1 %to &amp;n;
          %let val = %scan(&amp;svlist,&amp;i);
  
  
          create table work.&amp;val as
  
              select &amp;svselect,
  'SV' as souce
  from r_par.&amp;val
              where pnrq='0';
  
  
      %end;
  quit;
  %mend;
  
  %ovsql(&amp;ovlist);
  %svsql(&amp;SVlist);
  </code></pre>
  
  <p>The above works fine:</p>
  
  <p>As you can see, I bsically have two macro for dealing with 2 slightly different types of datasets. For this I use two lists:</p>
  
  <p>What I am wondering is if there is a way to consolidate the macros so that you could condition the program to run the "OV-macro" when a set in the list contain the letter OV in the name and the SV when a set in the list contain the letters SV. Can this be done?</p>
  
  <p>Here is my own attempt of which I have tried some variants, but not been able to get it to work, for reference.</p>
  
  <pre><code>options mprint;
  %macro PSQL(list);
  proc sql ;
  %let n=%sysfunc(countw(&amp;list));
      %do i=1 %to &amp;n;
          %let val = %scan(&amp;list,&amp;i);
           %let g= 
  %substr(&amp;val,7,2);
           ;
          %put(&amp;g);
  
           %if &amp;g=:SV %then %do; 
  
          create table work._&amp;val as
  
              select &amp;svselect,
  'SV' as souce
  from r_par.&amp;val
              where pnrq='0';
              %end;
              %else %if  &amp;g=:OV %then %do ;
                      create table work._&amp;val as
  
              select &amp;ovselect,
  'OV' as souce
  from r_par.&amp;val
              where pnrq='0';
      %end;
      %end;
  quit;
  %mend;
  
  %PSQL(R_PAR_SV_2012_VER3 R_PAR_OV_2012_VER3);
  </code></pre>
  
  <p>I would really appriciate any help. I am a bit of a novice when it comes to the sas macro language. Thanks in advance</p>