<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Looping over rows to perform search in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756285#M30058</link>
    <description>&lt;P&gt;Supposed I have a dataset work.id_score, and I intend to search it for a specific ID. If that ID is found in the dataset, then it will return me "Y". Otherwise, it will return a "N". I have written the codes below to do so if I only have one ID to search for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, should I have a list of ID, such as in work.id, then how should I go about searching for them at once? (Supposed that I have to make use of the codes written for the case of 1 ID of interest since this is a simplified example) Then desired result is work.id_results, where the results (Y or N) is returned as a new column next to the ID column. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id_score;
input ID$ score;
cards;
abc 10
def 20
ghi 30
abc 15
def 25
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let id_search = "abc";
&lt;BR /&gt;proc sql; create table work.id_count as
	select *
	from work.id_score
	where ID = &amp;amp;id_search;
quit;

proc sql; select case when count(*) &amp;gt; 0 then "Y" else "N" end as search_outcome
	from work.id_count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id;
input ID$;
cards;
abc
def
xyz
pqr
;
run;

data work.id_results;
input ID$ search_outcome$;
cards;
abc Y
def Y
xyz N
pqr N
;
run;&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;</description>
    <pubDate>Fri, 23 Jul 2021 18:30:41 GMT</pubDate>
    <dc:creator>jlin4</dc:creator>
    <dc:date>2021-07-23T18:30:41Z</dc:date>
    <item>
      <title>Looping over rows to perform search</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756285#M30058</link>
      <description>&lt;P&gt;Supposed I have a dataset work.id_score, and I intend to search it for a specific ID. If that ID is found in the dataset, then it will return me "Y". Otherwise, it will return a "N". I have written the codes below to do so if I only have one ID to search for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, should I have a list of ID, such as in work.id, then how should I go about searching for them at once? (Supposed that I have to make use of the codes written for the case of 1 ID of interest since this is a simplified example) Then desired result is work.id_results, where the results (Y or N) is returned as a new column next to the ID column. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id_score;
input ID$ score;
cards;
abc 10
def 20
ghi 30
abc 15
def 25
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let id_search = "abc";
&lt;BR /&gt;proc sql; create table work.id_count as
	select *
	from work.id_score
	where ID = &amp;amp;id_search;
quit;

proc sql; select case when count(*) &amp;gt; 0 then "Y" else "N" end as search_outcome
	from work.id_count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id;
input ID$;
cards;
abc
def
xyz
pqr
;
run;

data work.id_results;
input ID$ search_outcome$;
cards;
abc Y
def Y
xyz N
pqr N
;
run;&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jul 2021 18:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756285#M30058</guid>
      <dc:creator>jlin4</dc:creator>
      <dc:date>2021-07-23T18:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over rows to perform search</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756298#M30063</link>
      <description>&lt;P&gt;With your method, you will need to create a macro variable with the list of Ids that you want to search. Then loop through the list and apply proc sql for each element. This will be doable but lengthy. Instead, you can use merge as follows.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id_score;
input ID$ score;
cards;
abc 10
def 20
ghi 30
abc 15
def 25
;
run;

data work.id;
input ID$;
cards;
abc
def
xyz
pqr
;
run;

proc sort data=id_score; by id; run;
proc sort data=id; by id; run;

data search;
merge id_score(in=a) id(in=b);
   by id;
   if a and b then search_outcome='Y';
   else search_outcome='N';
   if b;
   keep id search_outcome;
run;

proc sort data=search nodupkey; by id; run;

proc print data=search; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jul 2021 19:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756298#M30063</guid>
      <dc:creator>Rydhm</dc:creator>
      <dc:date>2021-07-23T19:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over rows to perform search</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756299#M30064</link>
      <description>&lt;P&gt;There are multiple ways you could do it.&amp;nbsp; Have you considered using a hash table?&amp;nbsp; If you load your "look up" dataset, which would be work.id_score, into a hash table, you could do a read on the hash table.&amp;nbsp; If you get a zero return code, you would set the result to 'Y'.&amp;nbsp; If you get a &lt;STRONG&gt;non&lt;/STRONG&gt; zero return code, that would indicate that there is no match, and you would set the result to 'N'.&amp;nbsp; See example code, below, and, below that, the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id_score;
input ID$ score;
cards;
abc 10
def 20
ghi 30
abc 15
def 25
;
run;

data work.id;
input ID$;
cards;
abc
def
xyz
pqr
;
run;

DATA	WORK.id_results;
	DROP	_:;
	SET	WORK.ID;

	**------------------------------**;
	**	Define hash tables.			**;
	**------------------------------**;
	IF	_N_							=	1	THEN
		DO;
			DECLARE	HASH	Hsh_ID_Table			(ORDERED: 'A', MULTIDATA: 'N', DATASET: 'work.id_score');
							Hsh_ID_Table.DefineKey 	('ID');
							Hsh_ID_Table.DefineData	('ID');
							Hsh_ID_Table.DefineDone	();
		END;

	**------------------------------**;
	**	Look up ID.					**;
	**------------------------------**;
	_RC								=	Hsh_ID_Table.FIND();
	IF	_RC							=	0	THEN
		DO;
			Search_Outcome			=	'Y';
		END;
	ELSE
		DO;
			Search_Outcome			=	'N';
		END;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_0-1627068857735.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/61645i84BC9808A42C65A8/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1627068857735.png" alt="jimbarbour_0-1627068857735.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jul 2021 19:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756299#M30064</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-07-23T19:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over rows to perform search</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756300#M30065</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358095"&gt;@jlin4&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Supposed I have a dataset work.id_score, and I intend to search it for a specific ID. If that ID is found in the dataset, then it will return me "Y". Otherwise, it will return a "N". I have written the codes below to do so if I only have one ID to search for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, should I have a list of ID, such as in work.id, then how should I go about searching for them at once? (Supposed that I have to make use of the codes written for the case of 1 ID of interest since this is a simplified example) Then desired result is work.id_results, where the results (Y or N) is returned as a new column next to the ID column. Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id_score;
input ID$ score;
cards;
abc 10
def 20
ghi 30
abc 15
def 25
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let id_search = "abc";
&lt;BR /&gt;proc sql; create table work.id_count as
	select *
	from work.id_score
	where ID = &amp;amp;id_search;
quit;

proc sql; select case when count(*) &amp;gt; 0 then "Y" else "N" end as search_outcome
	from work.id_count;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.id;
input ID$;
cards;
abc
def
xyz
pqr
;
run;

data work.id_results;
input ID$ search_outcome$;
cards;
abc Y
def Y
xyz N
pqr N
;
run;&lt;/CODE&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here is a way that creates a numeric 1/0 outcome variable. Since SAS will treat 1 as true (or yes) and 0 as false (or no) for logic the 1/0 coding is usually a better way to go then character values like 'Y' and 'N'.&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as 
   select a.id, (a.id=b.id) as outcome
   from work.id as a left join
        (select distinct id from work.id_score) as b
        on a.id=b.id
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;You can get the count of "yes" with a sum function, the percent yes as a mean function result, any "yes" if the max is 1, all "yes" if the min is 1, all the same if the range is 0. Which could require some interesting coding to get with character values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to display Y/N Yes/No or similar than a custom format will do that just fine.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jul 2021 19:20:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756300#M30065</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-07-23T19:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: Looping over rows to perform search</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756309#M30066</link>
      <description>&lt;P&gt;The simpler the better &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select 
	id, 
	id in (select id from id) as outcome
from id_score;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jul 2021 20:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Looping-over-rows-to-perform-search/m-p/756309#M30066</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-07-23T20:13:34Z</dc:date>
    </item>
  </channel>
</rss>

