<?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: Concatenation require character operand in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230191#M5642</link>
    <description>&lt;P&gt;What's the problem with your code. You're populating SAS macro variables which are purely text based so it makes sense to create a text string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The generated SQL code still works with numeric variables so I don't see what the issue is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use keyword feedback "proc sql noprint &lt;STRONG&gt;feedback&lt;/STRONG&gt;;" you can see in the log that the generated SQL just looks fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select &lt;BR /&gt; EXAMPLE1.ID, &lt;BR /&gt; SUM(case &lt;BR /&gt; when EXAMPLE1.Months = 1 then EXAMPLE1.Balance&lt;BR /&gt; else 0&lt;BR /&gt; end) as Balance_01,&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;</description>
    <pubDate>Thu, 15 Oct 2015 22:43:10 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2015-10-15T22:43:10Z</dc:date>
    <item>
      <title>Concatenation require character operand</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230182#M5640</link>
      <description>&lt;P&gt;&amp;nbsp;data example1;&lt;BR /&gt;input ID Months Revenue Balance;&lt;BR /&gt;cards;&lt;BR /&gt;101 1 3 90&lt;BR /&gt;101 2 33 68&lt;BR /&gt;101 3 22 51&lt;BR /&gt;102 1 100 18&lt;BR /&gt;102 5 58 6&lt;BR /&gt;102 3 95 97&lt;BR /&gt;103 1 47 94&lt;BR /&gt;103 2 85 64&lt;BR /&gt;103 4 9 88&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to automate the following steps -&lt;/P&gt;
&lt;P&gt;sum(case when Months = 1 then Balance else 0 end) as Balance_1,&lt;/P&gt;
&lt;P&gt;sum(case when Months = 2 then&amp;nbsp;Balance else 0 end) as Balance_2,&lt;/P&gt;
&lt;P&gt;sum(case when Months = 3 then Balance else 0 end) as&amp;nbsp;Balance_3,&lt;/P&gt;
&lt;P&gt;sum(case when Months = 4 then Balance else 0 end) as Balance_4,&lt;/P&gt;
&lt;P&gt;sum(case when Months = 5&amp;nbsp;then Balance else 0 end) as Balance_5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code :&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct "sum(case when Months = "|| put(Months,z2.0) ||" then Balance else 0 end) as Balance_"|| put(Months,z2.0) &lt;BR /&gt;into :loop separated by "," &lt;BR /&gt;from example1;&lt;BR /&gt;create table Output as&lt;BR /&gt;select ID, &amp;amp;loop.&lt;BR /&gt;from example1&lt;BR /&gt;group by ID;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It works fine. The only problem with the code is it can only be run after converting the variable&amp;nbsp;to character with PUT function. If i remove PUT function, the code does not work. I want the code to be run without converting the variable to character operand.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Oct 2015 21:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230182#M5640</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2015-10-15T21:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation require character operand</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230191#M5642</link>
      <description>&lt;P&gt;What's the problem with your code. You're populating SAS macro variables which are purely text based so it makes sense to create a text string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The generated SQL code still works with numeric variables so I don't see what the issue is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use keyword feedback "proc sql noprint &lt;STRONG&gt;feedback&lt;/STRONG&gt;;" you can see in the log that the generated SQL just looks fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select &lt;BR /&gt; EXAMPLE1.ID, &lt;BR /&gt; SUM(case &lt;BR /&gt; when EXAMPLE1.Months = 1 then EXAMPLE1.Balance&lt;BR /&gt; else 0&lt;BR /&gt; end) as Balance_01,&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;</description>
      <pubDate>Thu, 15 Oct 2015 22:43:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230191#M5642</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-10-15T22:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation require character operand</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230224#M5644</link>
      <description>Why not use a proc transpose instead? It's built for automatic transposes like this and no macro variables or hardcoding. &lt;BR /&gt;UNTESTED CODE:&lt;BR /&gt;&lt;BR /&gt;proc sort data=have; by id months; run;&lt;BR /&gt;&lt;BR /&gt;proc transpose data=have out=want prefix=balance_;&lt;BR /&gt;id months;&lt;BR /&gt;var balance;&lt;BR /&gt;run;</description>
      <pubDate>Fri, 16 Oct 2015 04:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230224#M5644</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-10-16T04:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenation require character operand</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230241#M5646</link>
      <description>&lt;P&gt;Hi Ujjawal&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instaed of using the || operator you can also use one of the CAT functions, using these functions you do not have to convert numeric values to character, see code sample below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint feedback;
  select distinct
    cat("sum(case when Months = ", Months, " then Balance else 0 end) as Balance_", Months)
  into
    :loop separated by ","
  from
    example1
  ;
  %put NOTE: &amp;amp;=loop;
  create table Output as
    select
      ID
      , &amp;amp;loop.
    from example1
      group by
        ID
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bruno&lt;/P&gt;</description>
      <pubDate>Fri, 16 Oct 2015 07:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Concatenation-require-character-operand/m-p/230241#M5646</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2015-10-16T07:47:16Z</dc:date>
    </item>
  </channel>
</rss>

