I haven't ever been a big nested query user, but I'm trying my hand at this one for the sake of brevity in my code. I may have a parenthesis issue, but I've tried a few different ways of adding parenthesis with no luck. Using SAS EG version 7.11.
PROC SQL;
CREATE TABLE UNQ_MEMBER AS SELECT
DISTINCT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM (
SELECT
DISTINCT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1718
UNION
SELECT
DISTINCT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1819
ORDER BY CIN, ENDDTM
) MEMBERSHIP
GROUP BY CIN
HAVING ENDDTM = MAX(ENDDTM);
RUN;
The following are my separated queries, where I do get the correct results:
/*PULL AND CONCATENATE MEMBERSHIP FOR RELEVANT DATES*/
PROC SQL;
CREATE TABLE MEMBERSHIP AS
SELECT
DISTINCT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1718
UNION
SELECT
DISTINCT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1819
ORDER BY CIN, ENDDTM;
RUN;
/*GIVES TABLE W/ MOST RECENT DATES*/
PROC SQL;
CREATE TABLE UNQ_MEMBER AS SELECT
DISTINCT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBERSHIP
GROUP BY CIN
HAVING ENDDTM = MAX(ENDDTM);
RUN;
After trimming a few useless bits:
PROC SQL;
CREATE TABLE UNQ_MEMBER AS
SELECT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM (
SELECT
CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1718
UNION
SELECT
CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1819 )
GROUP BY CIN
HAVING ENDDTM = MAX(ENDDTM);
QUIT;
After trimming a few useless bits:
PROC SQL;
CREATE TABLE UNQ_MEMBER AS
SELECT CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM (
SELECT
CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1718
UNION
SELECT
CIN, INSCODE, REGION, MBR_COUNTY, ENDDTM
FROM MEMBER.MM1819 )
GROUP BY CIN
HAVING ENDDTM = MAX(ENDDTM);
QUIT;
Yep that's it thanks
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.