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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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...

 

Astounding
PROC Star

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.

MaximB91
Calcite | Level 5
Thank you! This actually works. I tweaked it to:
&Bucket. = cat('00',i+1,'.',' > ', &MinValue.+(i-1)*&Step., ' and <= ' ,&MinValue.+i*&Step.);
Astounding
PROC Star

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.);

MaximB91
Calcite | Level 5

This works even better. Thanks a lot!

Kurt_Bremser
Super User

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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