1

I am a beginner of macro. I modified a macro program and works. But, I have to write the following macro hundred times as follows.

%quint(dsn=cc1, var=MB, quintvar=MB5, num=111) ;    
%quint(dsn=cc1, var=MB, quintvar=MB5, num=112)  ;  
%quint(dsn=cc1, var=MB, quintvar=MB5, num=113) ;
-
- 
%quint(dsn=cc1, var=MB, quintvar=MB5, num=400)  ;  

Everytime I make a program for this job, num is recognized as an arugment instead of constant numbers. How can I make a short and easy one for this?

Joe
  • 62,789
  • 6
  • 49
  • 67
  • This is an often asked question here. You can easily do this in a Data Step. Joe's answer here (http://stackoverflow.com/questions/25545892/dynamically-call-macro-from-sas-data-step?s=1|1.2332) is probably the best summary of how to do that. There are other options and a quick google search will turn up lots of examples. – DomPazz Nov 04 '14 at 16:00
  • I think it's close enough to close. I would say the only thing that doesn't really cover is adding the constant number in, but that's easily done in the various solutions (usually by adding the number as a variable in a dataset). – Joe Nov 04 '14 at 20:34

2 Answers2

0

You can use RETAIN statement and CALL EXECUTE.

%macro quint (dsn, var, quintvar, num);
    %put &dsn;
    %put &var;
    %put &quintvar;
    %put #
%mend;

data _null_;
    retain numv 111;
    do until (numv = 400);
        call execute ('%quint(cc1, MB, MB5,'||numv||')');
        numv+1;
    end;
run;

This works.

Salva
  • 312
  • 2
  • 11
  • Thank you so much. It works! Since I am just a beginner of macro, ther are so many things that I don't understand when I read references. – keunsoo kim Nov 05 '14 at 02:07
0

Using call execute is usually ok, but it can be a bit fiddly getting the quoting right, and I find that it sometimes fails to process complex macros correctly. I prefer to use a macro approach, by re-writing the original macro to accept parameters for the loop start and end, or by writing a wrapper macro, e.g.

%macro quint (dsn, var, quintvar, num);
    %put &dsn;
    %put &var;
    %put &quintvar;
    %put #
%mend;

%macro run_quint(dsn, var, quintvar, num_from, num_to);
    %local i;
    %do i = &num_from %to &num_to;
        %quint(dsn=&dsn, var=&var, quintvar=&quintvar, num=&i)
    %end;
%mend;

%run_quint(dsn=cc1, var=MB, quintvar=MB5, num_from=111, num_to = 115);
user667489
  • 9,501
  • 2
  • 24
  • 35
  • It works as well. Thanks a lot. As a first step of learning macro, I really need direct advice as you show. Although there are similar questions and answers, I do not even recognize them and apply them to my case. – keunsoo kim Nov 05 '14 at 02:18