<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Insert a conditional string in macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795497#M255172</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301930"&gt;@SivaKizildag&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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&amp;nbsp; (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!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let mois=6;

%macro heyhey();
	%if &amp;amp;mois&amp;lt;10 %then %let mois = %Sysfunc(Catx(%Str("0"),&amp;amp;mois));
%mend;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;You do not need the CAT... series of data step functions to concatenate macro variables.&lt;/STRONG&gt;&amp;nbsp; To concatenate values in macro code all you have to do is type the values where you want them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to fix it with concatenation the code would just be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;mois&amp;lt;10 %then %let mois = 0&amp;amp;mois;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real fix is to use the Z format to make a macro variable that has the leading zeros.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mois = %sysfunc(putn(&amp;amp;mois,Z2.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Plus&lt;/STRONG&gt; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;And&lt;/STRONG&gt; you are also trying to use the wrong CAT... function.&amp;nbsp; The first argument to the CATX() function is the string you want to insert between the other arguments.&amp;nbsp; So you need to pass at least three arguments for it to do anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 10 Feb 2022 14:57:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-10T14:57:38Z</dc:date>
    <item>
      <title>Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795454#M255155</link>
      <description>&lt;P&gt;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&amp;nbsp; (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!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%let mois=6;

%macro heyhey();
	%if &amp;amp;mois&amp;lt;10 %then %let mois = %Sysfunc(Catx(%Str("0"),&amp;amp;mois));
%mend;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Feb 2022 10:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795454#M255155</guid>
      <dc:creator>SivaKizildag</dc:creator>
      <dc:date>2022-02-10T10:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795455#M255156</link>
      <description>&lt;P&gt;Try this instead&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mois=6;

%macro heyhey();
	%if &amp;amp;mois &amp;lt; 10 %then %let mois = %Sysfunc(putn(&amp;amp;mois., z2.));
%mend;

%heyhey()

%put &amp;amp;mois.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Feb 2022 10:39:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795455#M255156</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-10T10:39:56Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795456#M255157</link>
      <description>Still not, sorry. I think that the condition (%if &amp;amp;mois &amp;lt; 10) isn't even considered. I have a feeling that SAS thinks that MOIS is a string</description>
      <pubDate>Thu, 10 Feb 2022 10:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795456#M255157</guid>
      <dc:creator>SivaKizildag</dc:creator>
      <dc:date>2022-02-10T10:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795457#M255158</link>
      <description>&lt;P&gt;Did you run my code exactly as posted?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you don't need the &amp;lt; 10 check if you use the Z2. Format. You could simply do&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mois = %Sysfunc(putn(6, z2.));

%put &amp;amp;mois;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Feb 2022 10:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795457#M255158</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-10T10:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795458#M255159</link>
      <description>Yes indeed it works! Sorry ; I ran the whole code and it works! Thank you so much!</description>
      <pubDate>Thu, 10 Feb 2022 10:55:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795458#M255159</guid>
      <dc:creator>SivaKizildag</dc:creator>
      <dc:date>2022-02-10T10:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795459#M255160</link>
      <description>&lt;P&gt;Anytime &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 10:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795459#M255160</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-02-10T10:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: Insert a conditional string in macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795497#M255172</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301930"&gt;@SivaKizildag&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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&amp;nbsp; (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!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let mois=6;

%macro heyhey();
	%if &amp;amp;mois&amp;lt;10 %then %let mois = %Sysfunc(Catx(%Str("0"),&amp;amp;mois));
%mend;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;STRONG&gt;You do not need the CAT... series of data step functions to concatenate macro variables.&lt;/STRONG&gt;&amp;nbsp; To concatenate values in macro code all you have to do is type the values where you want them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to fix it with concatenation the code would just be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;mois&amp;lt;10 %then %let mois = 0&amp;amp;mois;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real fix is to use the Z format to make a macro variable that has the leading zeros.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mois = %sysfunc(putn(&amp;amp;mois,Z2.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Plus&lt;/STRONG&gt; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;And&lt;/STRONG&gt; you are also trying to use the wrong CAT... function.&amp;nbsp; The first argument to the CATX() function is the string you want to insert between the other arguments.&amp;nbsp; So you need to pass at least three arguments for it to do anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Feb 2022 14:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-a-conditional-string-in-macro-variable/m-p/795497#M255172</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-10T14:57:38Z</dc:date>
    </item>
  </channel>
</rss>

