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 !!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 2 replies
  • 805 views
  • 1 like
  • 2 in conversation