Hi there,
I would like to select the code that is both in ALLST and MISNG from the same data set. Any thoughts on how to go about it?
CODE | REGION |
516934 | ALLST |
516934 | MISNG |
328976 | ALLST |
987504 | MISNG |
489786 | ALLST |
489786 | MISNG |
987601 | ALLST |
348908 | MISNG |
890881 | ALLST |
314689 | ALLST |
314689 | MISNG |
348946 | ALLST |
787654 | MISNG |
975698 | ALLST |
975698 | MISNG |
107896 | ALLST |
/* UNTESTED CODE */
proc sql;
create table want as select
coalesce(a.code,b.code) as code
,a.region as region1
,b.region as region2
from have(where=(region='ALLST')) as a full join have(where=(region='MISNG')) as b
on a.code=b.code
where not missing(a.region) and not missing(b.region);
quit;
or, if data is consistent, subset it using by group first.variable and last.variable;
eg.
proc sort data=have; by code region; run; data want; set have; by code region; if not (first.code and last.code); proc print;run;
*or; data want; set have; by code region; if ((first.code and not last.code) and region eq 'ALLST') or ((last.code and not first.code) and region eq 'MISNG'); proc print;run;
Here a SQL way for doing this.
data have;
input CODE REGION $;
datalines;
516934 ALLST
516934 MISNG
328976 ALLST
987504 MISNG
489786 ALLST
489786 MISNG
987601 ALLST
348908 MISNG
890881 ALLST
314689 ALLST
314689 MISNG
348946 ALLST
787654 MISNG
975698 ALLST
975698 MISNG
107896 ALLST
;
proc sql;
select code, region
from have
where region in ('ALLST','MISNG')
group by code
having count(distinct region)=2
;
quit;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.