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-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 345 views
  • 1 like
  • 4 in conversation