Help using Base SAS procedures

can i use array to drop some variables?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

can i use array to drop some variables?

consider following codes:

array TestVar

  • _Character_;
  • do i = 1 to 3;

    drop TestVar;

    end;

    The purpose of this code is to drop the first three character variable. however, the code does not work for me. how should i edit the code to make it work?


    Accepted Solutions
    Solution
    ‎09-06-2011 11:12 PM
    PROC Star
    Posts: 7,486

    can i use array to drop some variables?

    Posted in reply to littlestone

    When you use one of the dictionary views in proc sql the libname and memname you specify both have to be in upper case.  Thus your line should have read:

        where libname='WORK' and memname='TEST'

    However, it wouldn't have worked anyway, as the sort would not have been in the expected order and proc sql, as far as I know, doesn't allow numeric collation.  Thus one extra step is needed, e.g.:

    data have;

      retain x1-x100 (100*1);

      do i=1 to 2;output;end;

    run;

    proc sql noprint;

      create table names as

        select name

         from dictionary.columns

           where libname='WORK' and

                 memname='HAVE'

      ;

    quit;

    proc sort data=names sortseq=linguistic  (numeric_collation=on);

      by name;

    run;

    proc sql noprint;

    select name into : list separated by ' '

      from names

    quit;

    data class;

    retain &list;

    set sashelp.class;

    run;

    data want;

    retain &list;

    set have;

    run;

    View solution in original post


    All Replies
    PROC Star
    Posts: 7,486

    can i use array to drop some variables?

    Posted in reply to littlestone

    You don't need the array.  Aren't you just trying to do something like the following?:

    data have;

      input var 1 (x y z) ($);

      cards;

    1 1 2 3

    2 5 2 1

    3 4 3 4

    ;

    data want;

      set have;

      drop _character_;

    run;

    Frequent Contributor
    Posts: 89

    can i use array to drop some variables?

    Thank you.

    "drop _Character_" will drop ALL character variables. I am just wondering if there is some way that can selectively drop some character variables.

    PROC Star
    Posts: 7,486

    can i use array to drop some variables?

    Posted in reply to littlestone

    I'm not sure how selectively you want.  From the pseudo code you had in your initial post, I presumed that it was all character variables.

    If you want to drop a range of variables you can always do something like the following, i.e., use a variable list:

    data have;

      input var 1 (x y z extra) ($);

      cards;

    1 1 2 3 1

    2 5 2 1 2

    3 4 3 4 3

    ;

    data want;

      set have;

      drop x--z;

    run;

    Frequent Contributor
    Posts: 89

    can i use array to drop some variables?

    thank you very much.

    I will refrase my quetions: how to reorder variables to be in alphabetical order?

    For example: supppose the original variables are (in order of):

    C100 B100 A100 C99 B99 A99 C98 B98 A98 ...... C1 B1 A1

    How can I change the order to:

    A1 A2 ... A100 B1 B2 ... B100 C1 C2 .. C100

    Reordering variables to be in alphabetical order
    Respected Advisor
    Posts: 3,799

    Re: can i use array to drop some variables?

    Posted in reply to littlestone

    Drop is not executable.  You will need to build the list first then use it in data step or data set option.  Here is one way to do it.

    25   proc transpose data=sashelp.heart(obs=0) out=vars;

    26      var _char_;

    27      run;

    NOTE: There were 0 observations read from the data set SASHELP.HEART.

    NOTE: The data set WORK.VARS has 7 observations and 2 variables.

    NOTE: PROCEDURE TRANSPOSE used (Total process time):

          real time           0.01 seconds

          cpu time            0.01 seconds

    28   proc sql noprint;

    29      select _name_ into :drop3 separated by ' '

    30         from vars(obs=3);

    31      quit;

    NOTE: PROCEDURE SQL used (Total process time):

          real time           0.00 seconds

          cpu time            0.00 seconds

    32      run;

    33   %put NOTE: DROP3=&drop3;

    NOTE: DROP3=Status DeathCause Sex

    Super User
    Posts: 10,041

    can i use array to drop some variables?

    Posted in reply to littlestone

    How about:

    proc sql noprint;
     select name into : list separated by ' '
      from dictionary.columns 
       where libname='SASHELP' and memname='CLASS'
        order by name;
    quit;
    data class;
     retain &list;
     set sashelp.class;
    run;
    

    Ksharp

    Frequent Contributor
    Posts: 89

    can i use array to drop some variables?

    thank you. I believe this is the answer i am looking for. but somehow i couldn't make it work:

    data test;

    input c2 b2 a2 c1 b1 a1;

    cards;

    1 2 3 4 5 6

    ;

    proc sql noprint;

    select name into : list separated by ' '

    from dictionary.columns

        where libname='work' and memname='test'

        order by name;

    quit;

    data new;

    retain &list;

      set test; 

    run;

    where am i wrong?

    Solution
    ‎09-06-2011 11:12 PM
    PROC Star
    Posts: 7,486

    can i use array to drop some variables?

    Posted in reply to littlestone

    When you use one of the dictionary views in proc sql the libname and memname you specify both have to be in upper case.  Thus your line should have read:

        where libname='WORK' and memname='TEST'

    However, it wouldn't have worked anyway, as the sort would not have been in the expected order and proc sql, as far as I know, doesn't allow numeric collation.  Thus one extra step is needed, e.g.:

    data have;

      retain x1-x100 (100*1);

      do i=1 to 2;output;end;

    run;

    proc sql noprint;

      create table names as

        select name

         from dictionary.columns

           where libname='WORK' and

                 memname='HAVE'

      ;

    quit;

    proc sort data=names sortseq=linguistic  (numeric_collation=on);

      by name;

    run;

    proc sql noprint;

    select name into : list separated by ' '

      from names

    quit;

    data class;

    retain &list;

    set sashelp.class;

    run;

    data want;

    retain &list;

    set have;

    run;

    Frequent Contributor
    Posts: 89

    can i use array to drop some variables?

    The code works now,

    Thank you so much, art297 and Ksharp. I really learned a lot from both of you.

    🔒 This topic is solved and locked.

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

    Discussion stats
    • 9 replies
    • 174 views
    • 6 likes
    • 4 in conversation