DATA Step, Macro, Functions and more

Conditional Array

Reply
Super Contributor
Posts: 702

Conditional Array


    a dataset has many variables and I want to selectively create arrays as follows:
 a b c d h i l m n
  array list1(*) not in (a b c)
  array list2(*) not in (l m n)
  array list3(*) not in (h i)

 

Is there a way SAs can handle this?

Super User
Posts: 22,823

Re: Conditional Array

Are you trying to create an array with list1 being the values d/h/i/l/m/n?

 

Super Contributor
Posts: 702

Re: Conditional Array

Yes

Super User
Posts: 6,534

Re: Conditional Array

Not knowing the details, it's possible you would be better off with just one array.  For example:

 

array mylist {*} _numeric_;

do _n_=1 to dim(mylist);

   if vname(mylist{_n_}) not in ('a' 'b' 'c') then do;

      *** logic that applies to LIST1;

   end;

   if vname(mylist{_n_}) not in ('l' 'm' 'n') then do;

      *** logic that applies to LIST2;

   end;

   if vname(mylist{_n_}) not in ('i' 'h') then do;

      *** logic that applies to LIST3;

   end;

end;

 

The comparison to the lists (such as 'a' 'b' 'c') is case sensitive.  You would need to be careful about the spelling of the variable names, or else just apply the LOWCASE function to make sure of what you are getting.

Super Contributor
Posts: 702

Re: Conditional Array

Posted in reply to Astounding

here a b c l m n h i are variables , so does the case come into play? 

Super User
Posts: 6,534

Re: Conditional Array

[ Edited ]

Yes, case is important.  The VNAME function retrieves the name of the variable.  Are you certain whether it will retrieve H or h as the variable name?  Safer but uglier:

 

if lowcase(vname(mylist{_n_})) not in ('h' 'i') then do;

PROC Star
Posts: 1,296

Re: Conditional Array

Posted in reply to Astounding

@SASPhile  How about

 

%let var=a b c d h i l m n;

%let list1=%sysfunc(compress(&var,abc));

%let list2=%sysfunc(compress(&var,lmn));

%let list3=%sysfunc(compress(&var,hi));

%put &list1;

%put &list2;

%put &list3;

 

data want;

array list1(*) &list1;

array list2(*) &list2;

array list3(*) &list3;

run;

Super Contributor
Posts: 702

Re: Conditional Array

Posted in reply to novinosrin

The idea is good. Some what happens is if the variables have common letters between them , they get compressed and we dont get desired output. Like if I have three variables projectid studyid siteid, they all get compreesed.

 

PROC Star
Posts: 1,296

Re: Conditional Array

Can you give me a simulated example close to your real plz?

Super User
Posts: 22,823

Re: Conditional Array

Look at the options iwthin the COMPRESS function to not have this happen.

Ask a Question
Discussion stats
  • 9 replies
  • 249 views
  • 2 likes
  • 4 in conversation