<?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 calculate median by year and country in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889302#M351347</link>
    <description>&lt;P&gt;Hi, I want to calculate the median by YEAR and COUNTRY, for two variables and then use this median in the next step to create a new variable. I'm using sql to do this, but something is off. Below is my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint; select YEAR, CNTRY, XREV, median(XREV) as medr from dataset; group by YEAR, CNTRY order by YEAR, CNTRY;
proc sql noprint; select YEAR, CNTRY, XASSTS, median(XASSTS) as meda from dataset; group by YEAR, CNTRY order by YEAR, CNTRY;
data want; set have; outr=ln(XREV/medr); outa=ln(XASSTS/meda);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is part of the log file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: The query requires remerging summary statistics back with the original data.
1133     !                                                                                    group by YEAR, CNTRY order by YEAR, CNTRY;
                                                                                              _____
                                                                                              180
ERROR 180-322: Statement is not valid or it is used out of proper order.

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can anyone give me some help here?&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Aug 2023 08:00:50 GMT</pubDate>
    <dc:creator>Satori</dc:creator>
    <dc:date>2023-08-15T08:00:50Z</dc:date>
    <item>
      <title>calculate median by year and country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889302#M351347</link>
      <description>&lt;P&gt;Hi, I want to calculate the median by YEAR and COUNTRY, for two variables and then use this median in the next step to create a new variable. I'm using sql to do this, but something is off. Below is my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint; select YEAR, CNTRY, XREV, median(XREV) as medr from dataset; group by YEAR, CNTRY order by YEAR, CNTRY;
proc sql noprint; select YEAR, CNTRY, XASSTS, median(XASSTS) as meda from dataset; group by YEAR, CNTRY order by YEAR, CNTRY;
data want; set have; outr=ln(XREV/medr); outa=ln(XASSTS/meda);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is part of the log file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: The query requires remerging summary statistics back with the original data.
1133     !                                                                                    group by YEAR, CNTRY order by YEAR, CNTRY;
                                                                                              _____
                                                                                              180
ERROR 180-322: Statement is not valid or it is used out of proper order.

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can anyone give me some help here?&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 08:00:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889302#M351347</guid>
      <dc:creator>Satori</dc:creator>
      <dc:date>2023-08-15T08:00:50Z</dc:date>
    </item>
    <item>
      <title>Re: calculate median by year and country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889311#M351350</link>
      <description>&lt;P&gt;Syntax ERROR happens because you ended the SELECT with a semicolon before the GROUP BY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The remerge will still happen because you have a variable in the SELECT which is not part of the GROUP BY. Remove the variables from the SELECT:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    YEAR, 
    CNTRY,
    median(XREV) as medr,
    ln(median(XREV)) as outr,
    median(XASSTS) as meda,
    ln(median(XASSTS)) as outa
  from dataset
  group by YEAR, CNTRY
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Aug 2023 09:32:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889311#M351350</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-15T09:32:41Z</dc:date>
    </item>
    <item>
      <title>Re: calculate median by year and country</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889312#M351351</link>
      <description>&lt;P&gt;Please read some papers and the documentation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* THE SELECT STATEMENT SAMPLE SYNTAX */
PROC SQL options;
   SELECT column(s)
   FROM table-name
   WHERE expression
   GROUP BY column(s)
   HAVING expression
   ORDER BY column(s);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A good paper to start with is: &lt;A title="Introduction to PROC SQL" href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi29/268-29.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi29/268-29.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 09:41:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-median-by-year-and-country/m-p/889312#M351351</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2023-08-15T09:41:37Z</dc:date>
    </item>
  </channel>
</rss>

