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

Hello!

My friend came to me with this issue and I wasn't sure how to unlock this one.  I mentioned I would come on SAS communities to see if anyone could help me figure this out.  The issue is he has a list that has numbers listed that when given dropped most of the leading zeros.  I wasn't sure how to pad these within a sysfunc, I do know that the put / input functions work outside of sysfunc, but I read that putc and inputc are needed for macro values.  

 

Thank you!

%let clients = '12345'; 
%let clientsb = %sysfunc(putc(%sysfunc(inputc(&clients,best7.)),z7.)); 
%put &clientsb;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

@IgawaKei29  Hi, I think you are perhaps after this?

 




%let clients = 12345; 
%let clientsb = %sysfunc(putn(%sysfunc(inputn(&clients,best7.)),z7.)); 
%put &=clientsb;

LOG:

27         %let clients = 12345;
28         %let clientsb = %sysfunc(putn(%sysfunc(inputn(&clients,best7.)),z7.));
29         %put &=clientsb;
CLIENTSB=0012345
30         

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

@IgawaKei29  Hi, I think you are perhaps after this?

 




%let clients = 12345; 
%let clientsb = %sysfunc(putn(%sysfunc(inputn(&clients,best7.)),z7.)); 
%put &=clientsb;

LOG:

27         %let clients = 12345;
28         %let clientsb = %sysfunc(putn(%sysfunc(inputn(&clients,best7.)),z7.));
29         %put &=clientsb;
CLIENTSB=0012345
30         
ballardw
Super User

Kind of need to show what the expected output should be.

 

Macro variables won't "drop" leading zero. Example:

%let a = 07;
%put a is: &a;

So the idea would be to do back to the source of the values with leading zeroes and what ever "removed" them.

Likely someone is reading a value that should be character, such as account numbers that often have leading zero, and not paying attention to how they are reading them. Such as abdicating responsibility and letting a procedure like Proc Import read them and create numeric variables. Prevention in the first place removes a whole lot of headaches "fixing" things later.

 

And doing such fixes in the macro language often indicates a design flaw for the entire process. Such as placing quotes as part of a macro variable.

%let x =23;
%let y = %sysfunc(putn(&x,z7.));

%put y is: &y.;

or

%let clients = 12345; 
%let clientsb = %sysfunc(putn(&clients,z7.)); 
%put &clientsb;

 Generally it is poor idea to make quotes part of the value of a macro variable.

Reeza
Super User

Ideally you'd be able to remove quotes from the macro variable which simplifies the problem. If you can't you can remove them using dequote.

 

%let clients = 12345; 
%let clientsb = %sysfunc(putn(&clients,z7.)); 
%put &clientsb;


%let clients = '12345'; 
%let clientsb = %sysfunc(putn(%sysfunc(dequote(&clients)), z7.));
%put &clientsb;

@IgawaKei29 wrote:

Hello!

My friend came to me with this issue and I wasn't sure how to unlock this one.  I mentioned I would come on SAS communities to see if anyone could help me figure this out.  The issue is he has a list that has numbers listed that when given dropped most of the leading zeros.  I wasn't sure how to pad these within a sysfunc, I do know that the put / input functions work outside of sysfunc, but I read that putc and inputc are needed for macro values.  

 

Thank you!

%let clients = '12345'; 
%let clientsb = %sysfunc(putc(%sysfunc(inputc(&clients,best7.)),z7.)); 
%put &clientsb;

 

Astounding
PROC Star

Like most posters, I don't believe that quotes are really part of the value.  And if they are, @Reeza has shown you a function to remove them.

 

At any rate, you don't need any %SYSFUNCs to get the result.  Just keep in mind that macro variable values are strings:

 

%let clients = 12345;
%let clientsb = %substr(0000000&clients, %length(&clients) + 1);
Tom
Super User Tom
Super User

Quotes are not needed in macro variables.  You need quotes in SAS code (and most languages) so the parser knows you meant a string and not a number. But the macro processor only works on text so no quotes are needed.  And if you include them they are part of the value.

 

The INPUTC() function needs a character informat since it generates a character result but you have given it a numeric informat.  The PUTC() functions needs a character format since it operates on character values but you have given it a numeric format.  Since the %SYSFUNC() macro function does not surface the error messages your code just silently does nothing to the original string.

 

Note that there is no need to use the INPUTN() function to turn a string of digits into a number.  Just use the string of digits in a place where a number is expected and it will be treated as a number.  Also note that there is no "BEST" informat.  If you use that name it is just translated into a request to use the default numeric informat.  BEST is the name of a format that makes an attempt to convert a number into a string of characters in the best possible way to fit the width specified.  But SAS only has one format for storing numbers, floating point, so there is no "best" way to convert the string into a number.  Instead the string is just converted into THE number it represents.

 

Just use the PUTN() function to treat the string of digits as a number and convert it into a fixed length string of digits that represents the same number only padded with leading zeros.

%let clients = 12345 ; 
%let clientsb = %sysfunc(putn(&clients,z7.)); 

 

IgawaKei29
Quartz | Level 8

Thank you @Tom @Astounding @Reeza @ballardw @novinosrin I really appreciate your insight on this.  Gives us both a perspective on macros and understanding how to deploy them properly. 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1481 views
  • 5 likes
  • 6 in conversation