BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sas_user_14
Calcite | Level 5

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 /*

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

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.

Sas_user_14
Calcite | Level 5
Thanks for answering !!