Help using Base SAS procedures

Declaring Variables in Arrays: Looking for an easier method

Reply
Occasional Contributor
Posts: 19

Declaring Variables in Arrays: Looking for an easier method

Hello,

I am using arrays to transform numeric variables in a SAS data set. I am seeking a quick way to declare transformed variables in arrays, such that a prefix is applied to the original variable name. In the below code, is there any way to declare the variables in the arrays t1var and t2var other than typing them out? I don't want to use t1-3, this would be faster but when my data set has lots of variables it is important that they retain their original names (plus the prefix) so that they can be easily identified.

Data data.Recoded_Variables;

  Set data.Variables;

  /*data.Variables contains 3 numeric

  variables: sales, expenses, employees */

  Array var

  • sales expenses employees;
  •   Array t1var

  • t1sales t1expenses t1employees;
  •   Array t2var

  • t2sales t2expenses t2employees;
  •   Do i=1 to dim(var);

           t1var= var/lag(var)-1;

           t2var= lag(t1var);

      End;

      Drop i;

    Run;

    Thank you!

    Will

    Super Contributor
    Posts: 1,636

    Re: Declaring Variables in Arrays: Looking for an easier method

    try this one:

    data variables;

    input id sales expenses employees ;

    cards;

    1 200 100 2

    2 300 200 3

    3 400 220 4

    ;

    proc sql noprint;

      select name

          ,cats('t1',name)

       ,cats('t2',name)

        into :name separated by ' '

            , :t1name separated by ' '

         , :t2name separated by ' '

      from dictionary.columns

        where libname='WORK' and memname='VARIABLES' and upcase(name) ne 'ID';/* note: libname and memname have to be capital letters */

    quit;

    data recoded_variables;

       set variables;

    Array var

  • &name;
  •   Array t1var

  • &t1name;
  •   Array t2var

  • &t2name;
  •   Do i=1 to dim(var);

           t1var= var/lag(var)-1;

           t2var= lag(t1var);

      End;

      Drop i;

    Run;

    proc print;run;

    Linlin

    Message was edited by:Linlin

    Respected Advisor
    Posts: 3,799

    Re: Declaring Variables in Arrays: Looking for an easier method

    This should work for any reasonable number of variables with names that arent too long for it to work properly.

    data variables;

       input sales expenses employees ;

       cards;

    200 100 2

    300 200 3

    400 220 4

    ;;;;

       run;

    %let vars=Sales-numeric-employees;

    proc transpose data=variables(obs=0) out=names;

       var &vars;

       run;

    proc sql noprint;

       select _name_ into :vars separated by ' '

       from names;

       quit;

       run;

    data names;

       length vname $32;

       set names;

       do vname = _name_,cats('T1',_name_),cats('T2',_name_);

          output;

          end;

       retain n 0;

       run;

    proc transpose data=names out=arrays(drop=_name_);

       var n;

       id vname;

       run;

    data want;

       if 0 then set arrays;

       Array _v

  • &vars;
  •    Array _t1

  • t1:;
  •    Array _t2

  • t2:;
  •    set variables;

       Do i=1 to dim(_v);

           _t1= _v/lag(_v)-1;

           _t2= lag(_t1);

           end;

       Drop i;

       Run;

    proc contents varnum;

    proc print;

       run;

    #    Variable       Type    Len

    1    sales          Num       8

    2    T1sales        Num       8

    3    T2sales        Num       8

    4    expenses       Num       8

    5    T1expenses     Num       8

    6    T2expenses     Num       8

    7    employees      Num       8

    8    T1employees    Num       8

    9    T2employees    Num       8

    Obs    sales    T1sales    T2sales    expenses    T1expenses    T2expenses    employees    T1employees    T2employees

    1      200      .            .          100           .             .            2           .                .

    2      300     0.50000       .          200          1.0            .            3          0.50000           .

    3      400     0.33333      0.5         220          0.1            1            4          0.33333          0.5

    Ask a Question
    Discussion stats
    • 2 replies
    • 188 views
    • 0 likes
    • 3 in conversation