BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dera
Obsidian | Level 7

Hi,

 

I would like to know how to use the find function with arrays.

 

Here is what I have : 

 

data want; set have;
	y1 = find(var, trim("&yes1"), 'i');
	y2 = find(var, trim("&yes2"), 'i');
	y3 = find(var, trim("&yes3"), 'i');
n1 = find(var, trim("&no1"), 'i'); n2 = find(var, trim("&no2"), 'i'); n3 = find(var, trim("&no3"), 'i'); run;

 

I would like to optimize my program and do something like this :

data want; set have;
	array y {*} y1-y3;
	do i = 1 to dim(y);
        y(i) = find(var, trim("&yes(i)"), 'i'); 
	end;

	array n {*} n1-n3;
	do i = 1 to dim(n);
        n(i) = find(var, trim("&no(i)"), 'i');
	end;
run;

 

Can anyone help me with that?

 

Thank you.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

If you insist on using macro-variables, then try:

data want; set have;
	array y {*} y1-y3;
	do i = 1 to dim(y);
          y(i) = find(var, symget(cats('yes', i)), 'i'); 
	end;

	array n {*} n1-n3;
	do i = 1 to dim(n);
          n(i) = find(var, symget(cats('no', i)), 'i');
	end;
run;

Properly created macro-variable don't have trailing blanks.

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

So apparently you have already created macro variables &yes1 &yes2 &yes3 and &no1 &no2 &no3. Then your loops would need this syntax

 

y(i) = find(var, trim("&&yes&i"), 'i'); 
--
Paige Miller
Astounding
PROC Star

What do you mean by "optimize my program" ?

 

If you have created your macro variables carefully (by removing leading and trailing blanks) you can make the program run faster by removing the TRIM function.

dera
Obsidian | Level 7
I mean that if I want to make it easier if I decide to add more macro variable. Paige Miller understood what I am looking for but his code does not work. Do you have idea?
PaigeMiller
Diamond | Level 26

@dera wrote:
I mean that if I want to make it easier if I decide to add more macro variable. Paige Miller understood what I am looking for but his code does not work. Do you have idea?

We can't help you if the only information you give us is "does not work". Please explain. Please show us the SASLOG. Give us information.

--
Paige Miller
PaigeMiller
Diamond | Level 26
%do i = 1 %to 3;
    y(&i) = find(var, trim("&&yes&i"), 'i'); 
%end;

Although I am starting to think that there must be easier ways to do this than mixing arrays and macro variables, but since we don't really know what you are doing, it's hard to advise.

--
Paige Miller
Astounding
PROC Star

From what you have posted so far, the program would be easier with no macro variables at all.  How do they make anything easier?

andreas_lds
Jade | Level 19

If you insist on using macro-variables, then try:

data want; set have;
	array y {*} y1-y3;
	do i = 1 to dim(y);
          y(i) = find(var, symget(cats('yes', i)), 'i'); 
	end;

	array n {*} n1-n3;
	do i = 1 to dim(n);
          n(i) = find(var, symget(cats('no', i)), 'i');
	end;
run;

Properly created macro-variable don't have trailing blanks.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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