Desktop productivity for business analysts and programmers

How to use the loop number as numeric operand?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to use the loop number as numeric operand?

Good day everyone,

 

I'm trying to write a code to automaticly group a variable in predefined buckets. In a last step I want to name the value (bucket name) based on the loop number, but somehow I'm not able to do this.

 

The code of this last step is as follows:

Data Want;
set Have;

/* If the observation has no bucket yet proceed to actions */
IF &Bucket.='' THEN DO;

	/* Go through as many loops as there are buckets */
	DO i=1 TO (2+(&MinValue.+&MaxValue.)/&Step.)-2;
		
		/* If the value is > the minimum value of the bucket and <= the max value, then this should be the bucket*/
		IF &Value. > &MinValue.+(i-1)*&Step. AND &Value. <= &MinValue.+ i*&Step. THEN 
			
			DO;	/* If the bucket number is < 10 use 00 as a prefix, <100 use 0 as a prefix and otherwise use no prefix */
				IF i + 1 < 10 THEN &Bucket.= "00%SYSEVALF(i+1). > %SYSEVALF(&MinValue.+(i-1)*&Step.) and <= %SYSEVALF(&MinValue.+i*&Step.)";

				IF i + 1 >= 10 and i+1 < 100 THEN &Bucket.= "0%SYSEVALF(i+1). > %SYSEVALF(&MinValue.+(i-1)*&Step.) and <= %SYSEVALF(&MinValue.+i*&Step.)";

				IF i + 1 >= 100 THEN &Bucket.= "%SYSEVALF(i+1). > %SYSEVALF(&MinValue.+(i-1)*&Step.) and <= %SYSEVALF(&MinValue.+i*&Step.)";
			END;
	END;
END;

run;

 

 

For example:

"00%SYSEVALF(&i.+1). > %SYSEVALF(&MinValue.+(&i.-1)*&Step.) and <= %SYSEVALF(&MinValue.+&i.*&Step.)";

Should result in: "002. > 0 and <= 10" if &Value. equals a number between 0 and 10.

 

 

However SAS doesn't treat "i" as a numeric value in the generation of the result, but as the character i. So the %SYSEVALF() function doesn't work.:

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

i+1

 

SAS does treat "i" as a numeric operand in the prior IF statement. I'm confused. Is there a solution for this?

 

Regards,

Maxim


Accepted Solutions
Solution
‎02-07-2018 10:00 AM
Super User
Posts: 6,935

Re: How to use the loop number as numeric operand?

[ Edited ]

You have correctly identified the problem.  Here's one way to approach a solution.

 

&bucket. = catx(' ', put(i, z3.), '>', &MinValue.+(i-1)*&Step., 'and <=' ,&MinValue.+i*&Step.);

 

It is conceivable that you need to tweak this, since the values for &MinValue and &Step are constant for the duration of the DATA step.  However, the formulas might work just as they are ... just one way to find out.

View solution in original post


All Replies
Super User
Super User
Posts: 9,855

Re: How to use the loop number as numeric operand?

First off, why all the macro code?  All that macro code in there is irrelevant - you can do these things in straight datastep - and it is also not explained, What is &Bucket. ?

Secondly why not use the procedure written to do this kind of thing;

http://documentation.sas.com/?docsetId=proc&docsetTarget=p0le3p5ngj1zlbn1mh3tistq9t76.htm&docsetVers...

 

Solution
‎02-07-2018 10:00 AM
Super User
Posts: 6,935

Re: How to use the loop number as numeric operand?

[ Edited ]

You have correctly identified the problem.  Here's one way to approach a solution.

 

&bucket. = catx(' ', put(i, z3.), '>', &MinValue.+(i-1)*&Step., 'and <=' ,&MinValue.+i*&Step.);

 

It is conceivable that you need to tweak this, since the values for &MinValue and &Step are constant for the duration of the DATA step.  However, the formulas might work just as they are ... just one way to find out.

New Contributor
Posts: 3

Re: How to use the loop number as numeric operand?

Posted in reply to Astounding
Thank you! This actually works. I tweaked it to:
&Bucket. = cat('00',i+1,'.',' > ', &MinValue.+(i-1)*&Step., ' and <= ' ,&MinValue.+i*&Step.);
Super User
Posts: 6,935

Re: How to use the loop number as numeric operand?

Glad it's working.  I hope you checked out the Z3 format.  It means you might need just one formula instead of 3 formulas for different ranges.  Something like:

 

&Bucket. = cat( put(i+1,z3.) || '.',' > ', &MinValue.+(i-1)*&Step., ' and <= ' ,&MinValue.+i*&Step.);

New Contributor
Posts: 3

Re: How to use the loop number as numeric operand?

Posted in reply to Astounding

This works even better. Thanks a lot!

Super User
Posts: 10,589

Re: How to use the loop number as numeric operand?

The macro functions are evaluated while the data step code is fetched for compilation, and see i as text and not as a variable name.

Don't use macro statements for actions that should happen when the data step runs.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 172 views
  • 0 likes
  • 4 in conversation