Hi,
I meet a problem.
while I want to insert a marco variable with zero at the front into a data.
I found that the zero will be clean.
Because the process of insert marco variable will change type by : marco variable-> num -> char.
And it will case the zero missing.
Example:
%let i = 007; data a; ax=put(&i. , $4); run;
*/output: 7 /*
When your macro variable is resolved, this code is the result:
data a;
ax=put(007 , $4);
run;
007 is a numeric literal with the value 7 (numbers do not have leading zeroes, leading zeroes can only be created by numeric formats).
But:
Maxim 3: Read the Log:
72
73 %let i = 007;
74
75 data a;
76 ax=put(&i. , $4);
_
85
76
ERROR 85-322: Expecting a format name.
ERROR 76-322: Syntax error, statement will be ignored.
77 run;
Your incorrect use of a format (missing dot) causes a syntax ERROR.
After correction, we get this:
73 %let i = 007; 74 75 data a; 76 ax=put(&i. , $4.); WARNING: Variable 007 has already been defined as numeric. 77 run;
which is a consequence of your use of a character format for a numeric value.
So we need to use a proper numeric format that displays leading zeroes, and with the correct number of digits:
%let i = 007;
data a;
ax=put(&i. , z3.);
run;
But since you want a character value, this is accomplished in a much much simpler way:
%let i = 007;
data a;
ax = "&i.";
run;
by directly creating a character value from your macro variable.
When your macro variable is resolved, this code is the result:
data a;
ax=put(007 , $4);
run;
007 is a numeric literal with the value 7 (numbers do not have leading zeroes, leading zeroes can only be created by numeric formats).
But:
Maxim 3: Read the Log:
72
73 %let i = 007;
74
75 data a;
76 ax=put(&i. , $4);
_
85
76
ERROR 85-322: Expecting a format name.
ERROR 76-322: Syntax error, statement will be ignored.
77 run;
Your incorrect use of a format (missing dot) causes a syntax ERROR.
After correction, we get this:
73 %let i = 007; 74 75 data a; 76 ax=put(&i. , $4.); WARNING: Variable 007 has already been defined as numeric. 77 run;
which is a consequence of your use of a character format for a numeric value.
So we need to use a proper numeric format that displays leading zeroes, and with the correct number of digits:
%let i = 007;
data a;
ax=put(&i. , z3.);
run;
But since you want a character value, this is accomplished in a much much simpler way:
%let i = 007;
data a;
ax = "&i.";
run;
by directly creating a character value from your macro variable.
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.