BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
warnost
Calcite | Level 5

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?

    1 ACCEPTED SOLUTION

    Accepted Solutions
    Linlin
    Lapis Lazuli | Level 10

    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

    6 REPLIES 6
    Linlin
    Lapis Lazuli | Level 10

    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;
  • warnost
    Calcite | Level 5

    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!

    Linlin
    Lapis Lazuli | Level 10

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

    The red parts have to be upcase.

    warnost
    Calcite | Level 5

    That worked! Thank you!

    data_null__
    Jade | Level 19

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

    TRANSFORM

    NONE

    LOG

    SQRT

    warnost
    Calcite | Level 5

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

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    Register now!

    How to Concatenate Values

    Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

    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
    • 6 replies
    • 2085 views
    • 3 likes
    • 3 in conversation