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.
Aviso importante
Los códigos y ejemplos proporcionados en WeAreCAS.eu son con fines educativos. Es imperativo no copiarlos y pegarlos ciegamente en sus entornos de producción. El mejor enfoque es comprender la lógica antes de aplicarla. Recomendamos encarecidamente probar estos scripts en un entorno de prueba (Sandbox/Dev). WeAreCAS no acepta ninguna responsabilidad por cualquier impacto o pérdida de datos en sus sistemas.
SAS y todos los demás nombres de productos o servicios de SAS Institute Inc. son marcas registradas o marcas comerciales de SAS Institute Inc. en los EE. UU. y otros países. ® indica registro en los EE. UU. WeAreCAS es un sitio comunitario independiente y no está afiliado a SAS Institute Inc.
Este sitio utiliza cookies técnicas y analíticas para mejorar su experiencia.
Saber más.