DATA Step, Macro, Functions and more

Macro Pass variable value to other macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 78
Accepted Solution

Macro Pass variable value to other macro

I really seems to be still learning these Macro...Could somebody please help :


I have two macros VDEVMAC and VDEVMAC1  ...Invoking VDEVMAC after passing values to it and then I have need to pass over the values/variables from it to VDEVMAC1 however it doesn't seems to be happening as the variable in VDEVMAC1 is Unresolved/uninitlized...

Here's my VDEVMAC program :

OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL MACROGEN;              

DATA _NULL_;                                                

%global thmvdev;                                            

%macro Vdevmac(THMVDEV=,THMVDEL=) ;                         

CALL SYMPUT('THMVDEV',TRIM(THMVDEV));                       

%PUT "VDEVMAC Program created the value of &THMVDEV" ;      

run;                                                        

%mend vdevmac;                                             

OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL MACROGEN;                      

%GLOBAL THMVDEV;                                                     

%macro Vdevmac1;                                                     

%PUT "VDEVMAC1 Program created the value of &THMVDEV" ;              

Data bvir_base;                                                      

    set pdb.bvir;                                                     

    attrib cluster  label='Cluster*id'                              ; 

    attrib dlibseqn label='Dist Lib Seq num'                        ; 

    attrib glibseqn label='Grid Lib Seq num'                        ; 

    attrib machserl label='Machine Serial'                          ; 

    attrib vdevinst label='Virt Dev Installed'                      ; 

    attrib devmntav label='Avg Virt  Dev Mounted'                   ; 

    attrib devmntmx label='Max Virt  Dev Mounted'                   ; 

    attrib devavgdl label='Device Average Delay'                    ; 

    attrib devmaxdl label='Device Maximum Delay'                     ;

    attrib devintdl label='Device Interval Delay %'                 ; 

*   Thresh_Max_Vdev = 70;                                             

*   Thresh_Max_Vdel = 10 ;                                           

Date = datepart(startime) ;                                      

Time = timepart(startime) ;                                      

format date date7. time time5. ;                                 

machserl = substr(machserl,4,5) ;                                

%if devmntmx > &THMVDEV %then %do ;                              

              output bvir_base;                                   

                               %end;                              

%mend vdevmac1;                                                 


Accepted Solutions
Solution
‎07-03-2014 08:05 AM
Super User
Posts: 7,868

Re: Macro Pass variable value to other macro

Since you never actually call the macros, all your SAS system ever "sees" is

OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL MACROGEN;

DATA _NULL_;

OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL MACROGEN;

- keep your scopes in line (eg. if the DATA statement is outside a macro, the associated RUN should also be outside, and vice versa)

- the macro processor is a generator of program text, it does NOT execute any statements. In your second macro, you have a completely meaningless statement:

%if devmntmx > &THMVDEV %then %do ;                              

             output bvir_base;                                   

                              %end;

In case the condition is not met, no output statement is generated. In that case, the data step will always output to the dataset named in the DATA statement, which also is bvir_base!

But the condition will always be met, because the letter "d" is always higher in the collating sequence than any number.

You most probably do not need the second macro at all, just do a standard data step comparison of devmntmx to &THMVDEV

What you wanted to do looks like this to me:

%let THMVDEV=any value that you want to test for;

Data bvir_base;                                                      

    set pdb.bvir;

    where devmntmx > &THMVDEV;

    attrib cluster  label='Cluster*id'                              ; 

    attrib dlibseqn label='Dist Lib Seq num'                        ; 

    attrib glibseqn label='Grid Lib Seq num'                        ; 

    attrib machserl label='Machine Serial'                          ; 

    attrib vdevinst label='Virt Dev Installed'                      ; 

    attrib devmntav label='Avg Virt  Dev Mounted'                   ; 

    attrib devmntmx label='Max Virt  Dev Mounted'                   ; 

    attrib devavgdl label='Device Average Delay'                    ; 

    attrib devmaxdl label='Device Maximum Delay'                     ;

    attrib devintdl label='Device Interval Delay %'                 ; 

*   Thresh_Max_Vdev = 70;                                             

*   Thresh_Max_Vdel = 10 ;                                           

Date = datepart(startime) ;                                      

Time = timepart(startime) ;                                      

format date date7. time time5. ;                                 

machserl = substr(machserl,4,5) ;                                

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Super User
Posts: 7,997

Re: Macro Pass variable value to other macro

VdevmacThis bit of code is wrong is several senses:

DATA _NULL_;                                                

      %global thmvdev;                                            

      %macro Vdevmac(THMVDEV=,THMVDEL=) ;                         

      CALL SYMPUT('THMVDEV',TRIM(THMVDEV));                       

      %PUT "VDEVMAC Program created the value of &THMVDEV" ;      

run;                                                        

%mend vdevmac;                

You have a datastep overlapping a macro definition first.  A global statement with the datastep.  Further more I can't see any place where the Vdevmac is actually called.  I would advise looking again at what you are trying to achieve and maybe coming at it from there.

Solution
‎07-03-2014 08:05 AM
Super User
Posts: 7,868

Re: Macro Pass variable value to other macro

Since you never actually call the macros, all your SAS system ever "sees" is

OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL MACROGEN;

DATA _NULL_;

OPTIONS MPRINT MLOGIC SYMBOLGEN SPOOL MACROGEN;

- keep your scopes in line (eg. if the DATA statement is outside a macro, the associated RUN should also be outside, and vice versa)

- the macro processor is a generator of program text, it does NOT execute any statements. In your second macro, you have a completely meaningless statement:

%if devmntmx > &THMVDEV %then %do ;                              

             output bvir_base;                                   

                              %end;

In case the condition is not met, no output statement is generated. In that case, the data step will always output to the dataset named in the DATA statement, which also is bvir_base!

But the condition will always be met, because the letter "d" is always higher in the collating sequence than any number.

You most probably do not need the second macro at all, just do a standard data step comparison of devmntmx to &THMVDEV

What you wanted to do looks like this to me:

%let THMVDEV=any value that you want to test for;

Data bvir_base;                                                      

    set pdb.bvir;

    where devmntmx > &THMVDEV;

    attrib cluster  label='Cluster*id'                              ; 

    attrib dlibseqn label='Dist Lib Seq num'                        ; 

    attrib glibseqn label='Grid Lib Seq num'                        ; 

    attrib machserl label='Machine Serial'                          ; 

    attrib vdevinst label='Virt Dev Installed'                      ; 

    attrib devmntav label='Avg Virt  Dev Mounted'                   ; 

    attrib devmntmx label='Max Virt  Dev Mounted'                   ; 

    attrib devavgdl label='Device Average Delay'                    ; 

    attrib devmaxdl label='Device Maximum Delay'                     ;

    attrib devintdl label='Device Interval Delay %'                 ; 

*   Thresh_Max_Vdev = 70;                                             

*   Thresh_Max_Vdel = 10 ;                                           

Date = datepart(startime) ;                                      

Time = timepart(startime) ;                                      

format date date7. time time5. ;                                 

machserl = substr(machserl,4,5) ;                                

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 248 views
  • 0 likes
  • 3 in conversation