DATA Step, Macro, Functions and more

Creating Variable Names dynamically

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Creating Variable Names dynamically

Hi,

I often have to transform large sets of variables for use in regression, and how I normally hand the task is to manually code arrays for each of the new transformed variables, similar to the this example:

data example;

  input A B C;

  array var

  • A B C;
  •   array change

  • r_A r_B r_C;
  •   do i = 1 to dim(change);

           change = var/lag(var) - 1;

      end;

      datalines;

    10 20 30

    15 5 60

    25 30 10

    run;

    This becomes very tedious when you have large numbers of variables. I was hoping to get to something like the following, but I have not been able to create functioning code. Not sure if a macro loop is the right way to go, been trying to use a combination of vname and cat functions but no luck so far.

    data example;

      input A B C;

      array var

  • _NUMERIC_;
  •   array change

  • /*code that adds the prefix "r_" to all variables in var[]*/;
  •   do i = 1 to dim(change);

      change = var/lag(var) - 1;

      end;

      datalines;

    10 20 30

    15 5 60

    25 30 10

    run;

    Any ideas?


    Accepted Solutions
    Solution
    ‎01-09-2013 12:07 PM
    Super Contributor
    Posts: 1,636

    Re: Creating Variable Names dynamically

    is the example helpful?

    proc sql noprint;
    select name into : names separated by ' '
       from dictionary.columns
        where libname="SASHELP" and memname="CLASS" and type="num";
    select cats('r_',name) into : r_names separated by ' '
       from dictionary.columns
        where libname="SASHELP" and memname="CLASS" and type="num";

    quit;
    data want;
      set sashelp.class;
      array var

  • &names;
      array change
  • &r_names;
      do i = 1 to dim(change);
           change = var/lag(var) - 1;
      end;
      drop i;
    proc print;run;
  • View solution in original post


    All Replies
    Solution
    ‎01-09-2013 12:07 PM
    Super Contributor
    Posts: 1,636

    Re: Creating Variable Names dynamically

    is the example helpful?

    proc sql noprint;
    select name into : names separated by ' '
       from dictionary.columns
        where libname="SASHELP" and memname="CLASS" and type="num";
    select cats('r_',name) into : r_names separated by ' '
       from dictionary.columns
        where libname="SASHELP" and memname="CLASS" and type="num";

    quit;
    data want;
      set sashelp.class;
      array var

  • &names;
      array change
  • &r_names;
      do i = 1 to dim(change);
           change = var/lag(var) - 1;
      end;
      drop i;
    proc print;run;
  • Occasional Contributor
    Posts: 19

    Re: Creating Variable Names dynamically

    I tried this code but I get the following error in my log:

    2335      array var

  • &names;
  •                        -

                           22

                           200

    WARNING: Apparent symbolic reference NAMES not resolved.

    2336      array change

  • &r_names;
  •                           -

                              22

                              200

    WARNING: Apparent symbolic reference R_NAMES not resolved.

    ERROR 22-322: Syntax error, expecting one of the following: a name, a numeric constant, $, (, ;,

                  _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_, _TEMPORARY_.

    ERROR 200-322: The symbol is not recognized and will be ignored.

    I am using the code exactly, just with sashelp and class replaced with the library and data set that I am using. I am not familiar with proc sql, is this the correct implementation?

    Thanks!

    Super Contributor
    Posts: 1,636

    Re: Creating Variable Names dynamically

    where libname="SASHELP" and memname="CLASS" and type="num";

    The red parts have to be upcase.

    Occasional Contributor
    Posts: 19

    Re: Creating Variable Names dynamically

    That worked! Thank you!

    Respected Advisor
    Posts: 3,777

    Re: Creating Variable Names dynamically

    Consider using new observations for the transformation.  With an ID to sort them.

    TRANSFORM

    NONE

    LOG

    SQRT

    Occasional Contributor
    Posts: 19

    Re: Creating Variable Names dynamically

    I will consider using new observations but it will require me to rethink the way I do the regression and other procedures. Thank you!

    ☑ This topic is SOLVED.

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

    Discussion stats
    • 6 replies
    • 301 views
    • 3 likes
    • 3 in conversation