Hi,
I need to select the first day of each month for multiple years. I have daily admissions for 10 years. I need this collapsed into one row for the first date of each month over the ten years. Ultimately, I need only 120 rows rather than 3650 rows.
I have summed daily admissions (to hospital) for each month of each year. This gives me one column with the total admissions by month/year, but a row for each day of that month. When I try proc sort with no dupkey by month(date) it fails at the nodupkey step.
/*Step 1 - works fine*/
proc sql;
create table WANT_1 as select *,
case when eventid>0 then count(eventid) else . end as monthly_admissions
from HAVE
group by month(evstdate);
run;
/*Step 2 - does not work fine*/
proc sort data=WANT_1 out=WANT_2 nodupkey ; by month(admission_date); run;
At step 2, I get this error message beneath "by month(admission_date)" :
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, -, :, DECENDING, DESCENDING, DESENDING, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.
33 proc sort data=count_month out=count_month_a nodupkey ; by month(admission_date); run;
_
200
ERROR 200-322: The symbol is not recognized and will be ignored.
Can anybody help with this? Many, many thanks!
You can't sort by an expression in proc sort. And you don't need to.
proc sql;
create table WANT as
select
intnx("month", evstdate, 0) as monthDate format=yymmdd10.,
count(eventId) as monthly_admissions
from HAVE
group by calculated monthDate;
run;
(untested)
Does the log have any messages about re-merging?
I think it should and you'll always get too many records with the select * approach. Try listing just the fields you want to keep explicitly including month. FYI - I don't think month is the correct grouping level unless you want monthly averages over the 10 years because I'm pretty sure that's what is happening. So you have a single average for all of January, not an average for January 2011, January 2012 etc.
@drshashlik wrote:
Hi,
I need to select the first day of each month for multiple years. I have daily admissions for 10 years. I need this collapsed into one row for the first date of each month over the ten years. Ultimately, I need only 120 rows rather than 3650 rows.
I have summed daily admissions (to hospital) for each month of each year. This gives me one column with the total admissions by month/year, but a row for each day of that month. When I try proc sort with no dupkey by month(date) it fails at the nodupkey step.
/*Step 1 - works fine*/
proc sql;
create table WANT_1 as select *,
case when eventid>0 then count(eventid) else . end as monthly_admissions
from HAVE
group by month(evstdate);
run;
/*Step 2 - does not work fine*/
proc sort data=WANT_1 out=WANT_2 nodupkey ; by month(admission_date); run;
At step 2, I get this error message beneath "by month(admission_date)" :
ERROR 22-322: Syntax error, expecting one of the following: a name, ;, -, :, DECENDING, DESCENDING, DESENDING, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.33 proc sort data=count_month out=count_month_a nodupkey ; by month(admission_date); run;
_
200
ERROR 200-322: The symbol is not recognized and will be ignored.
Can anybody help with this? Many, many thanks!
You can't sort by an expression in proc sort. And you don't need to.
proc sql;
create table WANT as
select
intnx("month", evstdate, 0) as monthDate format=yymmdd10.,
count(eventId) as monthly_admissions
from HAVE
group by calculated monthDate;
run;
(untested)
You can't use a FUNCTION such as MONTH() in a BY statement. That is causing your error.
Collapsed how?
It might help to show some example data and what you expect the output to look like.
I might start with a guess with something like
Proc freq data=have noprint;
where eventid>0;
tables evstdate / out=want;
format evstdate yymon. ;
run;
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.