BookmarkSubscribeRSS Feed
pacman94
Calcite | Level 5

I have the following dataset. What I would like to do is find NEW enteries based on ID and instance # into the new export but retain the new information

old:

IDgroupinstancetermterm_2
111AAAAone
12 indirect2CCCCseven
12 indirect3DDDDseven
211ASFDtwo
22 indirect2DDAthree
32 direct1ERQEone
411ABCDseven
412ABCDfive

for exmaple this is the new dataset that I would like to create. If ID and instance doesn't exist "old" dataset, then I would like to flagT the entries into the another dataset. new export:

IDgroupinstancetermterm_2
111AAAAone
12 indirect2CCCCseven
12 indirect3DDDDseven
12  indirect4DFDDtwo
211ASFDtwo
22 indirect2DDAthree
32 direct1ERQEone
411ABCDseven
412ABCDfive
42 direct3CCCCone
511ERQEseven

 

Want

 

IDgroupinstancetermterm_2flagT
111AAAAone0
12 indirect2CCCCseven0
12 indirect3DDDDseven0
12  indirect4DFDDtwo1
211ASFDtwo0
22 indirect2DDAthree0
32 direct1ERQEone0
411ABCDseven0
412ABCDfive0
42 direct3CCCCone1
511ERQEseven1

 

I did the following:

proc sql;

create table old_flag as 

select id, max(instance) as instance

from old group by id;

quit;

 

proc sql;

create table new_flag as

select id, max(instance) as instance

from new group by id;

quit;

 

not sure what to do next

2 REPLIES 2
HarrySnart
SAS Employee

Hi @pacman94 ,

 

Is this what you're after?

*Load data;
data work.old;
length id instance 8 group term term_2 $20;
input id  group instance  term  term_2 ;
infile datalines dlm="," dsd;
datalines;
1,	1,	1,	AAAA,	one
1,	2 indirect,	2,	CCCC,	seven
1,	2 indirect,	3,	DDDD,	seven
2,	1,	1,	ASFD,	two
2,	2 indirect,	2,	DDA,	three
3,	2 direct,	1,	ERQE,	one
4,	1,	1,	ABCD,	seven
4,	1,	2,	ABCD,	five
;

data work.new;
length id instance 8 group term term_2 $20;
input id  group instance  term  term_2 ;
infile datalines dlm="," dsd;
datalines;
1,	1,	1,	AAAA,	one
1,	2 indirect,	2,	CCCC,	seven
1,	2 indirect,	3,	DDDD,	seven
1,	2  indirect,	4,	DFDD,	two
2,	1,	1,	ASFD,	two
2,	2 indirect,	2,	DDA,	three
3,	2 direct,	1,	ERQE,	one
4,	1,	1,	ABCD,	seven
4,	1,	2,	ABCD,	five
4,	2 direct,	3,	CCCC,	one
5,	1,	1,	ERQE,	seven
;

*Find new records;

proc sql;
create table work.flag_1 as
select *, 1 as flagT from
(   select *
      from work.new
   except
   select *
      from work.old);
	quit;

*Add flag 0 to old records;
data work.old;
set work.old;
flagT = 0;
run;

*Merge tables;
data work.want;
set work.flag_1 work.old;
run;

HarrySnart_0-1647019480561.png

 

Ksharp
Super User
data work.old;
length id instance 8 group term term_2 $20;
input id  group instance  term  term_2 ;
infile datalines dlm="," dsd;
datalines;
1,	1,	1,	AAAA,	one
1,	2 indirect,	2,	CCCC,	seven
1,	2 indirect,	3,	DDDD,	seven
2,	1,	1,	ASFD,	two
2,	2 indirect,	2,	DDA,	three
3,	2 direct,	1,	ERQE,	one
4,	1,	1,	ABCD,	seven
4,	1,	2,	ABCD,	five
;

data work.new;
length id instance 8 group term term_2 $20;
input id  group instance  term  term_2 ;
infile datalines dlm="," dsd;
datalines;
1,	1,	1,	AAAA,	one
1,	2 indirect,	2,	CCCC,	seven
1,	2 indirect,	3,	DDDD,	seven
1,	2  indirect,	4,	DFDD,	two
2,	1,	1,	ASFD,	two
2,	2 indirect,	2,	DDA,	three
3,	2 direct,	1,	ERQE,	one
4,	1,	1,	ABCD,	seven
4,	1,	2,	ABCD,	five
4,	2 direct,	3,	CCCC,	one
5,	1,	1,	ERQE,	seven
;

data want;
 if _n_=1 then do;
   if 0 then set old;
   declare hash h(dataset:'old');
   h.definekey(all:'yes');
   h.definedone();
 end;
set new;
flagT=1;
if h.check()=0 then flagT=0;
run;

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