BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Satori
Quartz | Level 8

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:

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);

This is part of the log file:

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.

Can anyone give me some help here?

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Syntax ERROR happens because you ended the SELECT with a semicolon before the GROUP BY.

 

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:

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;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

Syntax ERROR happens because you ended the SELECT with a semicolon before the GROUP BY.

 

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:

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;
JosvanderVelden
SAS Super FREQ

Please read some papers and the documentation.

/* 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;

A good paper to start with is: https://support.sas.com/resources/papers/proceedings/proceedings/sugi29/268-29.pdf

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 553 views
  • 0 likes
  • 3 in conversation