Skip to content

The macroArray package [ver. 1.3.0]

Choose a tag to compare

@yabwon yabwon released this 13 Jan 14:48
· 2 commits to main since this release

The macroArray package [ver. 1.3.0]


Changes:

  • Four new parameters in the %do_over() macro: check, rephrase, trigger, and unq. See "Details" section below.
  • Minor updates in the %array() macro.
  • Documentation updated and refreshed.

SHA256 hash digest: F*FFF2C3D854F9B5677F561BA2EB6FAA2CCC652D81F6AF9473ADF0A4CE977E43F0


Details:

The new parameters were design to make work with the %do_over() macro easier.

New parameters

  • check= - Optional, indicates should a check for a macro corresponding to a macroarray be executed. If the macro does not exist warning is issued and the do_over stops. Default value 0 means: do not execute check.

  • rephrase= - Optional, this parameter allows for an alternative approach in providing the phrase to be looped over. The idea is to make writing the phrase string code more convenient and easy to grasp. The value is a string containing triggers (symbols) that are
    replaced by proper macroarray calls. For example, if a macroarray myArr has 7 values form varName1 to varName7 and you want
    to use them as arguments in code renaming variables, say rename old_varName1=new_varName1 ... ;, instead typing phrase: rename %do_over(myArr,phrase=%nrstr(old_%myArr(&_I_.)=new_%myArr(&_I_.))); you can type much easier rephrase: rename %do_over(myArr,rephrase=old_?=new_?);, and all ? will be replaced, under the hood, by calls to the macroarray. For easier debugging the do_over macro prints the rephrased string before and after change. When the do_over loops with multiple array, say myArrA, myArrB, and myArrC, then those arrays should be referred by ?1?, ?2?, and ?3? respectively. See trigger parameter definition to learn more. If both phrase and rephrase are used, the second takes precedence.

  • trigger= - Optional, a single byte character (symbol) used for marking macroarrays in the newly created phrase. Default value is ? symbol. When one macroarray is used, only the symbol should be used in rephrase= string. When multiple macroarrays are used then the symbol should surround a number identifying array, e.g. ?2?. See examples below for details.

  • unq= - Optional, indicates that the %unquote() macro function should be added around every macroarray call. Because of SAS internal behavior unq=1 is needed for certain cases when plain 4GL code is used in rephrase=. For example, let macro array myArr() has 3 values: A1, B2, and C3. When the following code is run: %do_over(myArr, rephrase=data ?_test; run;) without unq=1, SAS will create 4 data sets: A1, B2, C3, and _test, instead 3 data sets: A1_test, B2_test, and C3_test. Default value 1 means: add the %unquote(). See example below to learn more.


Examples:

EXAMPLE 1. Simpler multiple arrays looping with rephrase=.

  %array(alpha[*] j k l m n o p, vnames=Y, macarray=Y)
  %array( beta[&alphaN.], function = (2**_i_), macarray=Y)
  %array(gamma[&alphaN.] (1:&alphaN.), macarray=Y)
  
  %put >>%do_over(alpha)<<;
  %put >>%do_over(beta)<<;
  %put >>%do_over(gamma)<<;

  data test8;
    call streaminit(123);
    
    %do_over(           alpha   beta    gamma
           , rephrase = ?1?   = ?2?  +  ?3? * rand('Uniform'); output;
           , between  = put _all_;
            )
    put _all_;
  run;

EXAMPLE 2. Simpler multiple arrays looping with rephrase=, cont.
Create multiple datasets. Array alpha, beta, and gamma are
from the previous example.

  %do_over(alpha beta gamma
  , rephrase = 
      data ?1?_2;  
       call streaminit(?2?); 
       ?1?X = ?2? + ?3? * rand('Uniform');  
       output; 
      run;
  )

EXAMPLE 3. Simpler multiple arrays looping with rephrase=, cont.
Create multiple datasets using a macro. Array alpha, beta,
and gamma are from the previous example.
The %nrstr() is required to mask call to the %doit2() macro.
Default ? is replaced with @.

  %macro doit2(ds, var=a, val1=1, val2=2);
    data &ds._3; 
     call streaminit(&val1.);
     &var. = &val1. + &val2. * rand('Uniform'); 
     output;
    run;
  %mend doit2;

  %do_over(                    alpha                  beta        gamma
    , rephrase = %nrstr(%doit2(@1@, var = @1@, val1 = @2@, val2 = @3@))
    , trigger = @
  )

EXAMPLE 4. Simpler multiple arrays looping with rephrase=, cont.
Why the unq= is needed.

  %array(myArr[3] $ ("A1" "B2" "C3"), macarray=Y)

  %do_over(myArr, rephrase=data ?_testUNQ1; run;, unq=1)
  
  %do_over(myArr, rephrase=data ?_testUNQ0; run;, unq=0)

EXAMPLE 5. Simpler multiple arrays looping with rephrase=, cont.
Renaming variables is easy now.

  %array(V[*] a b c d e f g h, vnames=1, macarray=1)

  data test12;
    array x{*} %do_over(V) (1:&VN.);
  run;
  
  proc datasets nolist noprint lib=work;
    modify test12;
      rename 
        %do_over(V,rephrase = $=new_$,trigger=$)
      ;
    run;
  quit;
  
  data _null_;
    set test12;
    put _ALL_;
  run;