DATA Step, Macro, Functions and more

refer all elements in array in DROP statement in data step

Accepted Solution Solved
Reply
Occasional Contributor Xin
Occasional Contributor
Posts: 7
Accepted Solution

refer all elements in array in DROP statement in data step

Hello, 

I was wondering if someone can help me with a question working with array in data step. I was trying to drop all elements in an array after the reshape. Here is what i've tried and it gave error message that i can't refer the array this way in DROP statement. Is there a way to do it? Thanks in advance. - Xin

 

data tmp; 

   set tmp2; 

   array wide{*} _numeric_; 

   <other statement>

   drop wide{*}; 

run; 


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Posts: 5,085

Re: refer all elements in array in DROP statement in data step

This statement works IF you place it properly:

 

drop _numeric_;

 

It should appear immediately after the ARRAY statement.

 

In a DATA step, _numeric_ changes meaning as the DATA step creates more numeric variables.  So place the DROP statement after ARRAY, but before creating additional variables.

View solution in original post


All Replies
Super User
Posts: 10,508

Re: refer all elements in array in DROP statement in data step

If the purpose of this was to go from wide to narrow it likely is easier to KEEP the new variables. If there are lots of character variables to keep then use the _character_ variable list in the keep statement.

Occasional Contributor Xin
Occasional Contributor
Posts: 7

Re: refer all elements in array in DROP statement in data step

Yes, as you've point out, KEEP statement can be troublesome when there are lot of char varibles to keep. KEEP _character_ works well. Thanks a lot. 

Respected Advisor
Posts: 3,124

Re: refer all elements in array in DROP statement in data step

No, you can't do that as you have already figured out. The reason being non-executable statements such as 'Drop/Keep' works during the compiling stage, while Array statement works on execution stage. However, for the example you presented, you can do the following:

 

drop of _numeric_; 

 

 

Respected Advisor
Posts: 4,651

Re: refer all elements in array in DROP statement in data step

drop _numeric_;
but that will also drop any new numeric variable created in the datastep.
PG
Respected Advisor
Posts: 3,777

Re: refer all elements in array in DROP statement in data step

You don't need OF unless it is a variable name.  OF is the special function syntax.

Occasional Contributor Xin
Occasional Contributor
Posts: 7

Re: refer all elements in array in DROP statement in data step

The drop _numeric_ will drop the new variables i've created and won't work. 

Super Contributor
Posts: 254

Re: refer all elements in array in DROP statement in data step

Drop _numeric_ doesn't seem to be working. Here is a test:

data tmp; set sashelp.class; array wide[*] _numeric_; do i = 1 to dim(wide); put wide[i] =; end; drop _numeric_; put wide[*] =; run;
Occasional Contributor Xin
Occasional Contributor
Posts: 7

Re: refer all elements in array in DROP statement in data step

It worked on my computer. The three numberical variables are dropped in tmp as indicated by DROP statement. The last PUT statement will generate the three variables in log. 

Super Contributor
Posts: 254

Re: refer all elements in array in DROP statement in data step

Yes TMP is OK.

Solution
‎09-25-2015 06:23 AM
Super User
Posts: 5,085

Re: refer all elements in array in DROP statement in data step

This statement works IF you place it properly:

 

drop _numeric_;

 

It should appear immediately after the ARRAY statement.

 

In a DATA step, _numeric_ changes meaning as the DATA step creates more numeric variables.  So place the DROP statement after ARRAY, but before creating additional variables.

Occasional Contributor Xin
Occasional Contributor
Posts: 7

Re: refer all elements in array in DROP statement in data step

Thanks! This is the solution!

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 535 views
  • 1 like
  • 7 in conversation