En una Macro, todo es CADENA DE CARACTERES.
El usuario intentó esto:
%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 ! */
La función %eval (calculadora de enteros de macro) ve el texto '01Oct2007'd + 7. No sabe cómo convertir el literal '...'d a número. Por lo tanto, falla al realizar la suma.
2. La Solución: Pasar por el valor numérico (%SYSFUNC)
Como explica Linda (LAP), para hacer matemáticas en macro, debes trabajar con números puros, no con fechas formateadas.
Reformatear el número a una fecha legible solo para la visualización/uso final.
El Código Corregido
Así es como se transforma la lógica del usuario en una macro funcional:
%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. La Alternativa: %SYSEVALF
El experto Peter_C menciona una función más potente: %sysevalf.
A diferencia de %eval (que solo maneja enteros), %sysevalf (System Evaluation Floating) maneja números decimales, pero también es capaz de interpretar mejor ciertos literales de fecha si están entre comillas dobles.
Sin embargo, el método más robusto sigue siendo la conversión explícita a número a través de %sysfunc.
Regla de oro: El procesador de macros es "ciego" a los tipos de datos. Si quieres hacer matemáticas, dale números puros. Si quieres fechas, conviértelas a números antes de pasárselas.
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.