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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.