BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8


    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?

9 REPLIES 9
Reeza
Super User

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

 

SASPhile
Quartz | Level 8

Yes

Astounding
PROC Star

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.

SASPhile
Quartz | Level 8

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

Astounding
PROC Star

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;

novinosrin
Tourmaline | Level 20

@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;

SASPhile
Quartz | Level 8

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.

 

novinosrin
Tourmaline | Level 20

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

Reeza
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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