BookmarkSubscribeRSS Feed
Gladis6680
Obsidian | Level 7

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?

 

CODEREGION
516934ALLST
516934MISNG
328976ALLST
987504MISNG
489786ALLST
489786MISNG
987601ALLST
348908MISNG
890881ALLST
314689ALLST
314689MISNG
348946ALLST
787654MISNG
975698ALLST
975698MISNG
107896ALLST

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26
/* 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;
--
Paige Miller
A_Kh
Lapis Lazuli | Level 10

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; 

 

Patrick
Opal | Level 21

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;

Patrick_0-1696558098454.png

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 545 views
  • 1 like
  • 4 in conversation