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

Hi. I have this MOIS variable (month), which I input as a number. But I need it to have a "0" in front from january to september  (01 ... 09) and to have it as it (10 for october). I tried this code, but it doesnt seem to work, I still get 6 without the 0 in front when I need "06". Could you please help me ? Thanks! 

 

%let mois=6;

%macro heyhey();
	%if &mois<10 %then %let mois = %Sysfunc(Catx(%Str("0"),&mois));
%mend;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Did you run my code exactly as posted?

 

Also, you don't need the < 10 check if you use the Z2. Format. You could simply do

 

%let mois = %Sysfunc(putn(6, z2.));

%put &mois;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Try this instead

 

%let mois=6;

%macro heyhey();
	%if &mois < 10 %then %let mois = %Sysfunc(putn(&mois., z2.));
%mend;

%heyhey()

%put &mois.;
SivaKizildag
Fluorite | Level 6
Still not, sorry. I think that the condition (%if &mois < 10) isn't even considered. I have a feeling that SAS thinks that MOIS is a string
PeterClemmensen
Tourmaline | Level 20

Did you run my code exactly as posted?

 

Also, you don't need the < 10 check if you use the Z2. Format. You could simply do

 

%let mois = %Sysfunc(putn(6, z2.));

%put &mois;
SivaKizildag
Fluorite | Level 6
Yes indeed it works! Sorry ; I ran the whole code and it works! Thank you so much!
Tom
Super User Tom
Super User

@SivaKizildag wrote:

Hi. I have this MOIS variable (month), which I input as a number. But I need it to have a "0" in front from january to september  (01 ... 09) and to have it as it (10 for october). I tried this code, but it doesnt seem to work, I still get 6 without the 0 in front when I need "06". Could you please help me ? Thanks! 

 

%let mois=6;

%macro heyhey();
	%if &mois<10 %then %let mois = %Sysfunc(Catx(%Str("0"),&mois));
%mend;

You do not need the CAT... series of data step functions to concatenate macro variables.  To concatenate values in macro code all you have to do is type the values where you want them.

 

If you wanted to fix it with concatenation the code would just be:

%if &mois<10 %then %let mois = 0&mois;

 

The real fix is to use the Z format to make a macro variable that has the leading zeros.

%let mois = %sysfunc(putn(&mois,Z2.));

 

Plus the CAT... series of functions do not work well with %SYSFUNC() because they accept either numbers or characters as arguments and that means %SYSFUNC() has to try to figure out if your strings (everything in macro world is a string) look like numbers or not.

 

And you are also trying to use the wrong CAT... function.  The first argument to the CATX() function is the string you want to insert between the other arguments.  So you need to pass at least three arguments for it to do anything.

 

 

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
  • 860 views
  • 2 likes
  • 3 in conversation