DATA Step, Macro, Functions and more

HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 125
Accepted Solution

HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

My survey data has group variables such as A1-A10, B1-B2, C1-C5, D1-D15...so forth so on. And I'd like to exclude D: in following data step. D: has missing (.) . I don't need to shift D: by +1 to the right. 

I'm using SAS 9.4.  

 

data have1(drop=i); set have;
array shift[*] _numeric_;
do i=1 to dim(shift);
if shift(i) not in ('D:') then shift(i)=shift(i)+1;
end;
run;

data have1(drop=i); set have;
array shift[*] _numeric_;
do i=1 to dim(shift);
shift(i)=shift(i)+1;
end;
where _numeric_ not in ('D:'); run;

The codes above didn't work.  Any suggestions please? 

Thanks for your time. 


Accepted Solutions
Solution
‎06-21-2017 02:34 PM
Super User
Super User
Posts: 6,499

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

Following up on @ballardw post here is a method to create the array without the "D" variables, but keep then in the dataset.

You can take advantage of the fact that variable lists values depend on where they appear in the data step. The _NUMERIC_ variable list will only find the vairables that have already beed defined.  You can then SET the dataset again and read in all of the variables.

 

data want ;
  set have (drop=D:);
  array shift _numeric_;
  set have ;
  do over shift;
     shift=shift+1;
  end;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 6,499

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

[ Edited ]

I think you are asking how to conditionally execute based on the NAME of particular variable in a an ARRAY?

data want;
  set have;
  array shift _numeric_;
  do i=1 to dim(shift);
    if upcase(vname(shift(i))) ^=: 'D' then shift(i)=shift(i)+1;
  end;
  drop i;
run;
Super User
Posts: 10,483

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

You would have to test the name of the variable.

Something like this is one way:

 

if upcase(substr( vname(shift[i]), 1)) ne 'D' then <whatever>;

 

In the following data step use drop on the SET statement if you don't want it:

 

data want ;

    set have (drop= D: );

run;

The : says to consider all variables that start with D as a group and drop them. If you have other variables such as DATE then another

set have (drop= D1-D15) will use the listed variables and remove D1 through D15.

 

The concept here is variable list. There are several forms available in SAS.

Solution
‎06-21-2017 02:34 PM
Super User
Super User
Posts: 6,499

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

Following up on @ballardw post here is a method to create the array without the "D" variables, but keep then in the dataset.

You can take advantage of the fact that variable lists values depend on where they appear in the data step. The _NUMERIC_ variable list will only find the vairables that have already beed defined.  You can then SET the dataset again and read in all of the variables.

 

data want ;
  set have (drop=D:);
  array shift _numeric_;
  set have ;
  do over shift;
     shift=shift+1;
  end;
run;
Super User
Posts: 17,784

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

In this case I would suggest also consider not using the list _numeric_. 

 

You could either list them out separately or somehow.

 

array myvars (*) A: B: C: E: ... etc;

 

Frequent Contributor
Posts: 125

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

All worked out. What in general happens when you find more than one solutions worked out from multiple advisors? I probably have to post this in the community matter? 

Super User
Posts: 17,784

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION


SUNY_Maggie wrote:

All worked out. What in general happens when you find more than one solutions worked out from multiple advisors? I probably have to post this in the community matter? 


I don't think it really matters. Pick the solution that's the closest to what you need. If you end up compiling things from multiple people it's also ok to post the full solution in one place and mark that as the answer. The 'goal' of answering should be to inform someone searching as to which answer is correct for this problem, and yes there may be more than one. 

Frequent Contributor
Posts: 125

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

I thought of compiling once and then wondered if i then select my own compiling as for solution would look like taking the credit of others' work.
Super User
Posts: 17,784

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION


SUNY_Maggie wrote:
I thought of compiling once and then wondered if i then select my own compiling as for solution would look like taking the credit of others' work.

There are situations where that is appropriate and others where it's not. If you do that for all your questions, it's likely you're not making the correct decision but I think you're worrying about this too much. 

Frequent Contributor
Posts: 125

Re: HOW TO EXCLUDE GROUP VARIABLE FROM THE ARRAY FUNCTION

Ok. Thanks Reeza.
☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 156 views
  • 4 likes
  • 4 in conversation