In a Macro, everything is a STRING.
The user tried this:
%let i = '01Oct2007'd; /* &i contient le texte "'01Oct2007'd" */
%let i = %eval(&i + 7); /* ERREUR ! */
1
%let i = '01Oct2007'd;
2
/* &i contient le texte "'01Oct2007'd" */
3
%let i = %eval(&i + 7);
4
/* ERREUR ! */
The %eval function (macro integer calculator) sees the text '01Oct2007'd + 7. It doesn't know how to convert the '...'d literal into a number. It therefore fails to perform the addition.
2. The Solution: Using the Numeric Value (%SYSFUNC)
As Linda (LAP) explains, to do math in a macro, you must work with pure numbers, not formatted dates.
Re-format the number into a readable date only for final display/use.
The Corrected Code
Here's how to transform the user's logic into a functional macro:
%macro loop;
/* 1. On convertit le littéral en nombre (ex: 17440) grâce à PUTN ou INPUTN */
/* Note: Ici on utilise l'astuce de formater le littéral en nombre brut '8.' */
%let i = %sysfunc(putn('01Oct2007'd, 8.));
/* On convertit aussi la borne de fin */
%let fin = %sysfunc(putn('31Dec2007'd, 8.));
/* 2. La boucle compare maintenant deux nombres entiers */
%do %until (&i >= &fin);
/* 3. L'incrémentation fonctionne car &i est un entier */
%let i = %eval(&i + 7);
/* 4. Pour l'affichage, on re-transforme le nombre en date lisible */
%put %sysfunc(putn(&i, date9.));
%end;
%mend loop;
%loop;
1
%macro loop;
2
/* 1. On convertit le littéral en nombre (ex: 17440) grâce à PUTN ou INPUTN */
3
/* Note: Ici on utilise l'astuce de formater le littéral en nombre brut '8.' */
4
%let i = %sysfunc(putn('01Oct2007'd, 8.));
5
6
/* On convertit aussi la borne de fin */
7
%let fin = %sysfunc(putn('31Dec2007'd, 8.));
8
9
/* 2. La boucle compare maintenant deux nombres entiers */
10
%DO %until (&i >= &fin);
11
12
/* 3. L'incrémentation fonctionne car &i est un entier */
13
%let i = %eval(&i + 7);
14
15
/* 4. Pour l'affichage, on re-transforme le nombre en date lisible */
16
%put %sysfunc(putn(&i, date9.));
17
18
%END;
19
%mend loop;
20
21
%loop;
3. The Alternative: %SYSEVALF
The expert Peter_C mentions a more powerful function: %sysevalf.
Unlike %eval (which only handles integers), %sysevalf (System Evaluation Floating) handles floating-point numbers, but is also able to better interpret certain date literals if they are in double quotes.
However, the most robust method remains explicit conversion to a number via %sysfunc.
Golden Rule: The macro processor is "blind" to data types. If you want to do math, give it pure numbers. If you want dates, convert them to numbers before passing them to it.
Important Disclaimer
The codes and examples provided on WeAreCAS.eu are for educational purposes. It is imperative not to blindly copy-paste them into your production environments. The best approach is to understand the logic before applying it. We strongly recommend testing these scripts in a test environment (Sandbox/Dev). WeAreCAS accepts no responsibility for any impact or data loss on your systems.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.