BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
NKormanik
Barite | Level 11

 

Curious how SAS could efficiently do the following:

 

Given two text lists (say, of stock symbols) how can user subtract one list from the other?

 

Example:

 

AAAU

ACIM

ACWF

ACWX

ADRD

ADRE

ADRU

AGG

 

minus

 

ACIM

ACWF

AGG

 

We assume all members of List Two are included in List One.

 

From reading other's posts, Proc Compare and Proc SQL both look promising.

 

Any suggestions appreciated.

 

Nicholas Kormanik

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Or:

 

proc sql;
delete from list1
where stock in (select stock from list2);
quit;
PG

View solution in original post

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16
Please let us know if you are trying to identify the text not in either of the both list.
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

If the expected output is to have the list of text not in list2 but in list 1 the try the below code

 

data have1;
input list$;
cards;
AAAU
ACIM
ACWF
ACWX
ADRD
ADRE
ADRU
AGG
;

data have2;
input list$;
cards;
ACIM
ACWF
AGG
;

proc sort data=have1;
by list;
run;

proc sort data=have2;
by list;
run;

data want;
merge have1(in=a) have2(in=b);
by list;
if a and not b;
run;
Thanks,
Jag
PGStats
Opal | Level 21
proc sql;
create table list3 as
select stock from list1
except
select stock from list2;
quit;
PG
KachiM
Rhodochrosite | Level 12

It can be done without explicit sorting using Hash objects. Also we can use Proc Format. Here is the Hash way using the data sets given by Jag:

data want;
   if _n_ = 1 then do;
      if 0 then set have2;
      declare hash h(dataset:'have2');
      h.definekey('list');
      h.definedone();
   end;
   set have1;
   if h.check() ne 0;
run;

 

 

PGStats
Opal | Level 21

Or:

 

proc sql;
delete from list1
where stock in (select stock from list2);
quit;
PG
NKormanik
Barite | Level 11

 

No mention of Proc Compare yet?  Most examples out there that seemed reasonably close to my question used Proc Compare.

 

PGStats looks to be offering short and efficient code.  Nice tight quick solution.

 

Can anyone do better??

 

 

FreelanceReinh
Jade | Level 19

This is similar to @PGStats's "delete from" approach in that it removes observations from LIST1:

data list1;
modify list1 list2;
by stock;
remove;
run;

Like PROC SQL it does not require sorted input datasets -- despite the BY statement.

 

If LIST1 and LIST2 are the only datasets (in the WORK/USER library) whose names start with "LIST", the MODIFY statement could be written as

modify list:;
NKormanik
Barite | Level 11

 

Just for the record, here's my full code:

 

 


/*
Help given by PGStats.
Remove outdated, depricated stock symbols
from entire symbol list. Use MetaStock tester to isolate the bad symbols. Copy full symbol list to C:\0_SAS_1.
Name it ... as appropriate. Copy list of bad symbols to be removed
to C:\0_SAS_1. Name it "Remove.txt". */ data sas_1.ETF_List; infile "C:\0_SAS_1\ETF List.txt"; input Stocks $; run; data sas_1.Remove; infile "C:\0_SAS_1\Remove.txt"; input Stocks $; run; proc sql; delete from SAS_1.ETF_List where Stocks in (select Stocks from SAS_1.Remove); quit; proc export data=SAS_1.ETF_List outfile="C:\0_SAS_1\ETF List 2.txt"; putnames=no; run;

ed_sas_member
Meteorite | Level 14

Hi @NKormanik 

 

If have2 is very small compared to have1, using a hash will be in an efficient way to achieve you goal.

There is no need to sort those tables or to index them prior to do that.

 

Best,

 

data have1;
input list$;
cards;
AAAU
ACIM
ACWF
ACWX
ADRD
ADRE
ADRU
AGG
;
run;

data have2;
input list$;
cards;
ACIM
ACWF
AGG
;
run;

data want;
	if _n_ = 1 then do;
			declare hash h (dataset:'have2');
			h.definekey('list');
			h.definedone();
		end;
	set have1;
	if h.find() ne 0;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 9 replies
  • 1309 views
  • 10 likes
  • 6 in conversation