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

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?

    1 ACCEPTED SOLUTION

    Accepted Solutions
    art297
    Opal | Level 21

    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

    9 REPLIES 9
    art297
    Opal | Level 21

    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;

    littlestone
    Fluorite | Level 6

    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.

    art297
    Opal | Level 21

    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;

    littlestone
    Fluorite | Level 6

    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
    data_null__
    Jade | Level 19

    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

    Ksharp
    Super User

    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

    littlestone
    Fluorite | Level 6

    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?

    art297
    Opal | Level 21

    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;

    littlestone
    Fluorite | Level 6

    The code works now,

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

    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
    • 9 replies
    • 2533 views
    • 6 likes
    • 4 in conversation