<?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: proc sql create table problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943508#M369786</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_have
input year: best5. id: $20. factor: best20. medication best1.

dataline;
2020 | x1 | 5.19721817 | 1 
2017 | x2 | 14.06790123 | 1 
2019 | x3 | 9.03109676 | 2 
2017 | x4 | 5.15445063 | 1 
2015 | x5 | 5.94598581 | 1 
2013 | x6 | 4.43984891 | 1 
2014 | x6 | 4.38786664 | 1 
2015 | x6 | 4.41836734 | 1 
2018 | x6 | 4.6132294 | 1 
2017 | x7 | 6.76264097 | 1 
2020 | x8 | 11.8294265 | 1 
2021 | x9 | 6.2484863 | 1 
2021 | x9 | 6.2484863 | 1 
2022 | x1 |0 6.03128464 | 1 
2022 | x1 |0 6.03128464 | 2 
2012 | x11 | 8.47462116 | 1 
2020 | x12 | 4.30458199 | 1 
2018 | x13 | 4.39752762 | 2 
2018 | x13 | 4.39752762 | 2 
2022 | x14 | 6.3191628 | 2 
2022 | x14 | 6.3191628 | 2 
2018 | x15 | 9.42133597 | 1 
2018 | x15 | 9.42133597 | 1 
2019 | x15 |  8.4617257 | 1 
2020 | x15 | 6.8252753 | 1 
2022 | x16 | 6.84502614 | 2 
2017 | x17 | 11.11335597 | 1 
2020 | x18 | 8.3209465 | 2 
2022 | x18 | 8.06962168 | 2 
2022 | x19 | 4.79468183 | 2 
2015 | x20 | 6.82342541 | 1 
2016 | x20 | 7.47662275 | 1 
2016 | x20 | 7.47662275 | 1 
2016 | x20 | 7.47662275 | 1 
2016 | x20 |  7.47662275 | 1 
2016 | x20 | 7.47662275 | 1 
2022 | x21 | 8.73427034 | 1 
2016 | x22 |  6.13107083 | 1 
2017 | x22 | 6.7473995 | 1 
2019 | x23 | 6.24992605 | 1 
2018 | x24 | 11.54881266 | 2 
2018 | x24 | 11.54881266 | 2 
2021 | x25 | 9.28757739 | 2 
2012 | x26 | 8.32126661 | 1 
2020 | x27 | 6.48118077 | 2 
2022 | x27 | 6.60381036 | 2
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope this is fine. Thank you&lt;/P&gt;</description>
    <pubDate>Wed, 11 Sep 2024 15:13:43 GMT</pubDate>
    <dc:creator>Tamino</dc:creator>
    <dc:date>2024-09-11T15:13:43Z</dc:date>
    <item>
      <title>proc sql create table problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943496#M369782</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data set with several variable. Out of this, I want to create a separate data set with some existing variables and some new variables, that are produced out of an existing variable. This works pretty fine with this SAS code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table dataset_new as
   select id,
          year,
		  count(distinct medication) as n_medication,
          case 
             when count(distinct medication) = 1 then max(medication)
          end as medication_count_1,
          case 
             when count(distinct medication) = 2 then catx(', ', put(min(medication), 8.), put(max(medication), 8.))
          end as medication_count_2,
          case 
             when count(distinct medication) = 3 then '1, 2, 3'
          end as medication_count_3,
          case 
             when count(distinct medication) not in (1, 2, 3) then 'Unknown'
          end as medication_count_other
   from dataset_have
   group by id, year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;medication is eighter 1, 2, or 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I would like to include another variable ("factor") into the dataset_new, next to "id" and "year", I tried to addapt the code as followed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table dataset_new as
   select id,
          year,&lt;BR /&gt;          factor,
		  count(distinct medication) as n_medication,
          case 
             when count(distinct medication) = 1 then max(medication)
          end as medication_count_1,
          case 
             when count(distinct medication) = 2 then catx(', ', put(min(medication), 8.), put(max(medication), 8.))
          end as medication_count_2,
          case 
             when count(distinct medication) = 3 then '1, 2, 3'
          end as medication_count_3,
          case 
             when count(distinct medication) not in (1, 2, 3) then 'Unknown'
          end as medication_count_other
   from dataset_have
   group by id, year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But, now the dataset_new has way more records / rows than before and "id"s appear more than once per year (should only be once per year, as before).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is that, where is the fault? How can I fix the code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 14:29:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943496#M369782</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2024-09-11T14:29:49Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql create table problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943498#M369783</link>
      <description>&lt;P&gt;Please provide (a portion of) the data in data set DATASET_HAVE, following these &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt;. Do NOT attach files.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 14:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943498#M369783</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-09-11T14:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql create table problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943508#M369786</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_have
input year: best5. id: $20. factor: best20. medication best1.

dataline;
2020 | x1 | 5.19721817 | 1 
2017 | x2 | 14.06790123 | 1 
2019 | x3 | 9.03109676 | 2 
2017 | x4 | 5.15445063 | 1 
2015 | x5 | 5.94598581 | 1 
2013 | x6 | 4.43984891 | 1 
2014 | x6 | 4.38786664 | 1 
2015 | x6 | 4.41836734 | 1 
2018 | x6 | 4.6132294 | 1 
2017 | x7 | 6.76264097 | 1 
2020 | x8 | 11.8294265 | 1 
2021 | x9 | 6.2484863 | 1 
2021 | x9 | 6.2484863 | 1 
2022 | x1 |0 6.03128464 | 1 
2022 | x1 |0 6.03128464 | 2 
2012 | x11 | 8.47462116 | 1 
2020 | x12 | 4.30458199 | 1 
2018 | x13 | 4.39752762 | 2 
2018 | x13 | 4.39752762 | 2 
2022 | x14 | 6.3191628 | 2 
2022 | x14 | 6.3191628 | 2 
2018 | x15 | 9.42133597 | 1 
2018 | x15 | 9.42133597 | 1 
2019 | x15 |  8.4617257 | 1 
2020 | x15 | 6.8252753 | 1 
2022 | x16 | 6.84502614 | 2 
2017 | x17 | 11.11335597 | 1 
2020 | x18 | 8.3209465 | 2 
2022 | x18 | 8.06962168 | 2 
2022 | x19 | 4.79468183 | 2 
2015 | x20 | 6.82342541 | 1 
2016 | x20 | 7.47662275 | 1 
2016 | x20 | 7.47662275 | 1 
2016 | x20 | 7.47662275 | 1 
2016 | x20 |  7.47662275 | 1 
2016 | x20 | 7.47662275 | 1 
2022 | x21 | 8.73427034 | 1 
2016 | x22 |  6.13107083 | 1 
2017 | x22 | 6.7473995 | 1 
2019 | x23 | 6.24992605 | 1 
2018 | x24 | 11.54881266 | 2 
2018 | x24 | 11.54881266 | 2 
2021 | x25 | 9.28757739 | 2 
2012 | x26 | 8.32126661 | 1 
2020 | x27 | 6.48118077 | 2 
2022 | x27 | 6.60381036 | 2
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope this is fine. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 15:13:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943508#M369786</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2024-09-11T15:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql create table problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943509#M369787</link>
      <description>There is a fault with the delimiter with id "x10"</description>
      <pubDate>Wed, 11 Sep 2024 15:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943509#M369787</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2024-09-11T15:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql create table problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943510#M369788</link>
      <description>&lt;P&gt;You need to either include FACTOR in the GROUP BY list.&amp;nbsp; Or apply some aggregate function to it (like the COUNT() function you have).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which value of FACTOR do you want when there are multiple observations per GROUP BY grouping?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming that you want to include it in the BY grouping.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_have;
  infile datalines dlm='|' ;
  input year id :$20. factor medication ;
datalines;
2020|x1|5.19721817|1 
2017|x2|14.06790123|1 
2019|x3|9.03109676|2 
2017|x4|5.15445063|1 
2015|x5|5.94598581|1 
2013|x6|4.43984891|1 
2014|x6|4.38786664|1 
2015|x6|4.41836734|1 
2018|x6|4.6132294|1 
2017|x7|6.76264097|1 
2020|x8|11.8294265|1 
2021|x9|6.2484863|1 
2021|x9|6.2484863|1 
2022|x1|6.03128464|1 
2022|x1|6.03128464|2 
2012|x11|8.47462116|1 
2020|x12|4.30458199|1 
2018|x13|4.39752762|2 
2018|x13|4.39752762|2 
2022|x14|6.3191628|2 
2022|x14|6.3191628|2 
2018|x15|9.42133597|1 
2018|x15|9.42133597|1 
2019|x15|8.4617257|1 
2020|x15|6.8252753|1 
2022|x16|6.84502614|2 
2017|x17|11.11335597|1 
2020|x18|8.3209465|2 
2022|x18|8.06962168|2 
2022|x19|4.79468183|2 
2015|x20|6.82342541|1 
2016|x20|7.47662275|1 
2016|x20|7.47662275|1 
2016|x20|7.47662275|1 
2016|x20|7.47662275|1 
2016|x20|7.47662275|1 
2022|x21|8.73427034|1 
2016|x22|6.13107083|1 
2017|x22|6.7473995|1 
2019|x23|6.24992605|1 
2018|x24|11.54881266|2 
2018|x24|11.54881266|2 
2021|x25|9.28757739|2 
2012|x26|8.32126661|1 
2020|x27|6.48118077|2 
2022|x27|6.60381036|2
;

proc sql;
create table dataset_new as
   select id
        , year
        , factor
        , count(distinct medication) as n_medication
        , case 
             when count(distinct medication) = 1 then max(medication)
          else . end as medication_count_1
        , case 
             when count(distinct medication) = 2 
             then catx(', ', put(min(medication), 8.), put(max(medication), 8.))
          else ' ' end as medication_count_2
        , case 
             when count(distinct medication) = 3 then '1, 2, 3'
          else ' ' end as medication_count_3
        , case 
             when count(distinct medication) not in (1, 2, 3) then 'Unknown'
          else ' ' end as medication_count_other
   from dataset_have
   group by id ,year ,factor
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1726068561230.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/100220i4B5028737E5F4D5E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1726068561230.png" alt="Tom_0-1726068561230.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 15:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943510#M369788</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-09-11T15:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql create table problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943604#M369813</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By including FACTOR in the GROUP BY list, I get the output I was looking for.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 08:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-create-table-problem/m-p/943604#M369813</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2024-09-12T08:57:22Z</dc:date>
    </item>
  </channel>
</rss>

