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?
Are you trying to create an array with list1 being the values d/h/i/l/m/n?
Yes
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.
here a b c l m n h i are variables , so does the case come into play?
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;
@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;
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.
Can you give me a simulated example close to your real plz?
Look at the options iwthin the COMPRESS function to not have this happen.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.