Skip to content

Instantly share code, notes, and snippets.

@dcomtois
Created April 20, 2016 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomtois/3589a46c7328b9693ddfe2da0db22332 to your computer and use it in GitHub Desktop.
Save dcomtois/3589a46c7328b9693ddfe2da0db22332 to your computer and use it in GitHub Desktop.
SAS Macro to easily assign dates to macro variables. Macro SAS pour définir facilement des variables macro dates.
/* ========================================================== */
/* Routine Macro %letdate */
/* ========================================================== */
/* */
/* Affecte une date à une variable macro */
/* et affiche sa valeur formattée au log. La donnée */
/* enregistrée reste une date SAS (numérique) ou non, */
/* selon le paramètre fmt fourni. */
/* */
/* Arguments: */
/* */
/* nom = nom de la variable macro, sans le & */
/* val = valeur à affecter, pouvant inclure une ou */
/* plusieurs fonctions de dates SAS */
/* <fmt> = Format à appliquer à la valeur. Numérique (8. */
/* par défaut). Doit être compatible avec la */
/* valeur (val) calculée (donc éviter d'attribuer */
/* un format de date SAS à une valeur calculée */
/* par exemple avec YEAR() or MONT() */
/* */
/* Exemples : */
/* */
/* %letdate(hier, today()-1) */
/* %letdate(annee, year(today()), 4.) */
/* %letdate(annee, today(), YEAR.) */
/* %letdate(moisCourant, month(today()), z2.) */
/* %letdate(DebutMois, intnx("month",today(),0, "begin")) */
/* %letdate(finAnDer, intnx("year",today(),-1,"end")) */
/* %letdate(finMoisDer, intnx("month",today(),-1, "end")) */
/* */
/* Programmé par: Dominic Comtois, Jan-Fév 2016 */
/* dcomtois@statconseil.com */
/* */
/* ---------------------------------------------------------- */
/* Modifications (date, programmeur, description) */
/* */
/* */
/* */
/* ========================================================== */
%MACRO letdate(nom /* Nom de la variable macro à définir (ex: DebutMois) */,
val /* Valeur de la variable macro (ex: today()) */,
fmt /* Format de date SAS à appliquer à la valeur (ex: nldate.) */ );
/* Déclaration des variables locales */
%local saveOptions;
/* Vérification des arguments */
%if %length(&nom) = 0 or
%length(&val) = 0 %then %do;
%put ERROR: Un des paramètres (nom ou val) est manquant;
%abort cancel;
%end;
/* Si "fmt" n'est pas spécifié, on le fixe à 8. */
%if %length(&fmt)=0 %then %let fmt = 8.;
/* Sauvegarder et modifier l'option notes */
%let saveOptions = %sysfunc(getoption(notes));
options nonotes;
data _null_;
format &nom &fmt;
&nom = &val;
call symputx("&nom", put(&nom, &fmt), 'g');
%if &fmt=8. and &nom gt 16000 %then %do;
put ' *** Variable macro ' "&nom = " &nom "(" &nom DDMMYYS10. ") ***";
%end;
%else %do;
put ' *** Variable macro ' "&nom = " &nom "***";
%end;
run;
/* Restaurer l'option notes/nonotes */
options &saveOptions;
%MEND;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment