<?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: Constructing string variables with if-statements using values from %let statements at the beginn in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897847#M354862</link>
    <description>Thanks.</description>
    <pubDate>Mon, 09 Oct 2023 17:55:09 GMT</pubDate>
    <dc:creator>dlavery1</dc:creator>
    <dc:date>2023-10-09T17:55:09Z</dc:date>
    <item>
      <title>Constructing string variables with if-statements using values from %let statements at the beginning</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897843#M354858</link>
      <description>&lt;P&gt;Hello, I have a program I'm running every month, so I've set up %let statements at the beginning of my program for whoever is running this to change. I'd like to construct other variables from these values, and so far I can do this for numeric variables fine, but I'm struggling with string variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's what I'm doing:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;******************THESE LINES NEEDS TO BE CHANGED EVERY MONTH*****************;
%let year = 2023;
%let month = 08;
**********************************************************************************;

*---- this section constructs program-wide values from the month and year input above -------;

%let current_month = &amp;amp;year. *100 + &amp;amp;month.; *gets a numeric value in the 6-digit BLS format: e.g. 202002 for Feb 2020;
%let end_month = (&amp;amp;year. - 1) * 100 + (&amp;amp;month. - 1);

*%let length month_str $8.;
%let month_str = "";
if &amp;amp;month. = "01" then &amp;amp;month_str = "January";
else if &amp;amp;month. = "02" then &amp;amp;month_str = "February";
else if &amp;amp;month. = "03" then &amp;amp;month_str = "March";
else if &amp;amp;month. = "04" then &amp;amp;month_str = "April";
else if &amp;amp;month. = "05" then &amp;amp;month_str = "May";
else if &amp;amp;month. = "06" then &amp;amp;month_str = "June";
else if &amp;amp;month. = "07" then &amp;amp;month_str = "July";
else if &amp;amp;month. = "08" then &amp;amp;month_str = "August";
else if &amp;amp;month. = "09" then &amp;amp;month_str = "September";
else if &amp;amp;month. = "10" then &amp;amp;month_str = "October";
else if &amp;amp;month. = "11" then &amp;amp;month_str = "November";
else if &amp;amp;month. = "12" then &amp;amp;month_str = "December";&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The month_str section is not working. I think it's due to the if statements being outside a data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All suggestions or insights are appreciated!&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 17:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897843#M354858</guid>
      <dc:creator>dlavery1</dc:creator>
      <dc:date>2023-10-09T17:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: Constructing string variables with if-statements using values from %let statements at the beginn</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897844#M354859</link>
      <description>&lt;P&gt;Are you asking how to convert a month number into a month name?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Might be easiest to convert it to a DATE first.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let year = 2023;
%let month = 08;

%let month_string =%sysfunc(mdy(&amp;amp;month,1,&amp;amp;year),monname.);

%put &amp;amp;=year &amp;amp;=month &amp;amp;=month_string;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;4580   %put &amp;amp;=year &amp;amp;=month &amp;amp;=month_string;
YEAR=2023 MONTH=08 MONTH_STRING=August
&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Oct 2023 17:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897844#M354859</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-09T17:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Constructing string variables with if-statements using values from %let statements at the beginn</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897845#M354860</link>
      <description>&lt;P&gt;ADVICE: Do not work with dates and calendar time periods as character strings. This will always lead to inefficient programming. Work with dates and calendar items as valid SAS numeric date values, and then use built-in functions and formats and informats. Valid SAS numeric date values are the number of days since 01JAN1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Note: nothing needs to be changed each month */
%let today=%sysfunc(today());
%let current_month=%sysfunc(putn(&amp;amp;today,yymmn6.));
%let end_month=%sysfunc(intnx(month,&amp;amp;today,-12,b));
%let month_str=%sysfunc(putn(&amp;amp;today,monname12.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why don't your commands work for &amp;amp;month_str? Here's the first one&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if &amp;amp;month. = "01" then &amp;amp;month_str = "January";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IF command does not work outside of a DATA step. If you are doing this in a macro, you could use %IF, as shown below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if &amp;amp;month. = "01" %then %let &amp;amp;month_str = "January";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This won't work either, because in the case of January you would have set &amp;amp;MONTH to have a value of 01, which is not equal to "01". Can you see why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But anyway, all these IF statements are completely unnecessary if you have valid SAS date values, and you use the MONNAME format, as shown above.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2023 17:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897845#M354860</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-09T17:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Constructing string variables with if-statements using values from %let statements at the beginn</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897846#M354861</link>
      <description>Perfect, thank you so much!</description>
      <pubDate>Mon, 09 Oct 2023 17:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897846#M354861</guid>
      <dc:creator>dlavery1</dc:creator>
      <dc:date>2023-10-09T17:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Constructing string variables with if-statements using values from %let statements at the beginn</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897847#M354862</link>
      <description>Thanks.</description>
      <pubDate>Mon, 09 Oct 2023 17:55:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Constructing-string-variables-with-if-statements-using-values/m-p/897847#M354862</guid>
      <dc:creator>dlavery1</dc:creator>
      <dc:date>2023-10-09T17:55:09Z</dc:date>
    </item>
  </channel>
</rss>

