BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Whether we can use array in drop or keep option?
4 REPLIES 4
deleted_user
Not applicable
Yep, would be nice - doesn't work though. Lets see what SAS comments are.

If you're having problem with long variable lists and you know the order in your file (variable number) you can specify firstVar--lastVar which will select all variables in between the two specified.
JasonS_SAS
SAS Employee
Ashutosh, let me show what SAS can do then we'll discuss what it can't.

The DROP= data set option and DROP statement support variable lists to drop a set of variables. For instance, to drop all the variables from an output data set that begin with the prefix "X" one would code:

[pre]
DATA OUT( DROP=X: );
[/pre]

To drop the variables X1 through X10, one would code:

[pre]
DATA OUT( DROP=X1-X10 );
[/pre]

Variable lists work in many situations where a programmer would like to drop an array of variables.

The situation where variable lists break down is when an array of variables do not have a common prefix. As in:

[pre]
ARRAY A
  • X Y Z;
    [/pre]

    In this case using DROP=X--Z may drop X, Y, and Z. I say may because if the variables aren't ordering in the PDV as X, then Y, then Z, the variables you think you're dropping will not be dropped. I'd rather code DROP=X Y Z than use X--Z because maintaining a variable ordering in the PDV can be tricky and obfuscate the code.

    If you have a situation where variable lists don't work well, a workaround is to place the array variables in a macro variable. As in this code:

    [pre]
    %let ArrayVars = x y z;
    data out(drop=&ArrayVars.);
    array a
  • &ArrayVars.;
    set in;
    run;
    [/pre]

    This program will create an array consisting of the variables X, Y, and Z and will drop them from the output data set.

    The reason arrays aren't supported with the DROP= or KEEP= option is that the options are not processed by the DATA step. Rather, they are processed by the engine that is used to read or write the data set. The engine is aware of the variables to be read or written, so it can drop or keep them. The engine is not aware of DATA step arrays, so it would not recognize a name as an array. The separation between the DATA step and the engine is what keeps DROP= or KEEP= from handling arrays.

    -- Jason Secosky, SAS R&D / DATA Step
  • deleted_user
    Not applicable
    Ashutosh and Jason,

    I have just started with a system that has many, many fields - Ashutosh you might be in the same boat. Correct me if I'm wrong here.

    We don't have a situation of X Y Z but rather 100's of fields, which makes programs lengthy and difficult to manage. Arrays make short work of repeated calculations and macro variables containing the variable names also shortcut a lot of it. But whatever route you take, you still end up with these long, unwieldy lists somewhere.

    I'm taking the attitude of more rows less columns - trying the reduce the variable lists to categories in columns. Use of proc transpose can interchange between the tables. Taking this different approach makes programming much easier and shorter - have you tried thinking in this way? I've also returned a 75% space saving with this method - this is the business motivation of taking this route. Its often difficult justifying code changes without any obvious end-result.

    Jason your thoughts?
    JasonS_SAS
    SAS Employee
    Data organization should be given critical thought when designing a solution. The way data are arranged will affect the complexity of the implementation and performance.

    Views provide a mechanism for rearranging data without replicating them on disk. When different applications rely on the same data, yet each implementation would prefer a different layout, views often provide a sufficient solution.

    -- Jason Secosky, SAS R&D / DATA Step

    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!

    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
    • 4 replies
    • 976 views
    • 0 likes
    • 2 in conversation