BookmarkSubscribeRSS Feed
warnost
Calcite | Level 5

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

    2 REPLIES 2
    Linlin
    Lapis Lazuli | Level 10

    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

    data_null__
    Jade | Level 19

    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

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    Find more tutorials on the SAS Users YouTube channel.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

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