This article explains how to distinguish a null (empty) macro variable from a macro variable containing a period, and how to perform these tests correctly.
%put The variable contains something (LENGTH > 0).;
4
5
%END;
6
Warning: If %let a = .;, then %length(&a) is 1. The test above will therefore consider that the variable is not empty.
Method 2: Direct comparison
Another common method is to compare the variable to an empty string.
%if &maVar = %then %do;
%put The variable is empty.;
%end;
1
%IF &maVar = %THEN %DO;
2
3
%put The variable is empty.;
4
5
%END;
6
Note: This method is sometimes less readable than using %length, but it is functional.
Common syntax errors to avoid
When writing these conditions, pay attention to the structure of your %if / %then / %else blocks.
The semicolon error after %else
A classic mistake is to place a semicolon immediately after the %else, which terminates the statement prematurely.
Note : Incorrect:
/* The semicolon after %else prevents the execution of the next block */
%if %length(&a) %then %do;
%put Exists;
%end;
%else;
%put Does not exist;
1
/* The semicolon after %else prevents the execution of the next block */
2
%IF %LENGTH(&a) %THEN %DO;
3
%put Exists;
4
%END;
5
%ELSE;
6
%put Does not exist;
Note : Correct:
%if %length(&a) %then %do;
%put Exists;
%end;
%else %do;
%put Does not exist;
%end;
1
%IF %LENGTH(&a) %THEN %DO;
2
%put Exists;
3
%END;
4
%ELSE %DO;
5
%put Does not exist;
6
%END;
The macro language manipulates text.
A period (.) is a character, not a missing value in the macro sense.
Use %if %length(&var) = 0 to test for a null (empty) value.
To delve deeper into the subject of missing and logical values in macros, the works of Chung and King on the subject are solid technical references.
Avertissement important
Les codes et exemples fournis sur WeAreCAS.eu sont à but pédagogique. Il est impératif de ne pas les copier-coller aveuglément sur vos environnements de production. La meilleure approche consiste à comprendre la logique avant de l'appliquer. Nous vous recommandons vivement de tester ces scripts dans un environnement de test (Sandbox/Dev). WeAreCAS décline toute responsabilité quant aux éventuels impacts ou pertes de données sur vos systèmes.
SAS et tous les autres noms de produits ou de services de SAS Institute Inc. sont des marques déposées ou des marques de commerce de SAS Institute Inc. aux États-Unis et dans d'autres pays. ® indique un enregistrement aux États-Unis. WeAreCAS est un site communautaire indépendant et n'est pas affilié à SAS Institute Inc.
Ce site utilise des cookies techniques et analytiques pour améliorer votre expérience.
En savoir plus.