Search code examples
sassas-macro

Create a variable inside WHEN-DO statement SAS


Want to create 2 macro variables "list" and "list2" depending on prod's value but it always returns the last iteration values.

Thanks

%let prod=WC;
SELECT ;

  WHEN (WC = &prod)
    DO; 
        %let list = (60 , 63 ); 
        %let list2= ("6A","6B","6C") ;
    END;

  WHEN (MT = &prod)
    DO;
        %let list = (33 , 34);
        %let list2= ("3A","3B");
    END;

OTHERWISE;

END;

RUN;
``

Solution

  • The macro processor works before the generated code is passed to SAS to interpret. So your code is evaluated in this order:

    %let prod=WC;
    %let list = (60 , 63 ); 
    %let list2= ("6A","6B","6C") ;
    %let list = (33 , 34);
    %let list2= ("3A","3B");
    data ...
    SELECT ;
      WHEN (WC = &prod) DO; 
      END;
      WHEN (MT = &prod) DO;
      END;
      OTHERWISE;
    END;
    ...
    RUN;
    

    To set macro variables from a running data step use the CALL SYMPUTX() function. Also are you really trying to compare the variable WC to the variable MT? Does the data in your data step even have those variables? Or did you want to compare the text WC to the text MT?

    when ("WC" = "&prod") do;
      call symputx('list','(60,63)');
      call symputx('list2','("6A","6B","6C")') ;
    end;