<?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 Keep only certain observations in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616026#M18842</link>
    <description>&lt;P&gt;I am trying to edit a table down by selecting only the observations with certain entries. I want a table where the only observations are the ones where the variable CODE is equal to 59400, 59410, 59510, 59515, or 59610 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA want;&lt;BR /&gt;SET have;&lt;BR /&gt;if CODE =&amp;nbsp;&lt;BR /&gt;59400 59410 59510 59515 59610&lt;BR /&gt;then keep ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But for some reason I keep getting a table with 0 observations. Does anyone know why this is?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jan 2020 19:35:00 GMT</pubDate>
    <dc:creator>marleeakerson</dc:creator>
    <dc:date>2020-01-08T19:35:00Z</dc:date>
    <item>
      <title>Keep only certain observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616026#M18842</link>
      <description>&lt;P&gt;I am trying to edit a table down by selecting only the observations with certain entries. I want a table where the only observations are the ones where the variable CODE is equal to 59400, 59410, 59510, 59515, or 59610 .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA want;&lt;BR /&gt;SET have;&lt;BR /&gt;if CODE =&amp;nbsp;&lt;BR /&gt;59400 59410 59510 59515 59610&lt;BR /&gt;then keep ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But for some reason I keep getting a table with 0 observations. Does anyone know why this is?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 19:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616026#M18842</guid>
      <dc:creator>marleeakerson</dc:creator>
      <dc:date>2020-01-08T19:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only certain observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616028#M18843</link>
      <description>Assuming the codes are numeric you're looking for IN&lt;BR /&gt;&lt;BR /&gt;ie if code in (59400 59410 59510 ... ) then keep;&lt;BR /&gt;&lt;BR /&gt;if the variables are character the values in the parentheses need to have quotes:&lt;BR /&gt;&lt;BR /&gt;if code in('59400' '59410' ... );</description>
      <pubDate>Wed, 08 Jan 2020 19:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616028#M18843</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-01-08T19:36:42Z</dc:date>
    </item>
    <item>
      <title>Re: Keep only certain observations</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616033#M18844</link>
      <description>&lt;P&gt;There are several ways to do this in SAS. I've listed a few in my code below. My personal favorite is to remove my 'code' list into a custom format (option 3 below) that way it is in one spot in case I ever need to update it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Assuming code is numeric, otherwise encapsulate each code in '' (i.e. '59400' vs. 59400)*/
data have;
	input code;
	datalines;
1
59515
59400
5
59410
59510
;
run;

data desired_output;
	input code;
	datalines;
59515
59400
59410
59510
;
run;

/*Option 1*/
data opt1;
	set have;

	if code in (59400 59410 59510 59515); /*equivalent to: if code in (59400 59410 59510 59515) then output; */
run;

/*Option 2: place in set (where=) -- only reads in observations that match your criteria*/
data opt2;
	set have(where=(code in (59400 59410 59510 59515)));
run;

/*Option 3: remove logic from data step and place in custom format*/
proc format;
	value mycodes
		59400, 59410, 59510, 59515="1"
		other="0"
	;
run;

data opt3a;
	set have;

	if put(code,mycodes.); /*equivalent to: if put(code,mycodes.)="1" then output; */
run;

data opt3b;
	set have(where=(put(code,mycodes.)="1"));
run;

/*Option 4: remove logic from data step and place in macro variable*/
%let mycodes = (59400 59410 59510 59515);

data opt4a;
	set have;

	if code in &amp;amp;mycodes; /*equivalent to: if put(code,mycodes.)=1 then output; */
run;

data opt4b;
	set have(where=(code in &amp;amp;mycodes));
run;

/*Checking that results=desired_results*/
proc compare base=desired_output compare=opt1;
proc compare base=desired_output compare=opt2;
proc compare base=desired_output compare=opt3a;
proc compare base=desired_output compare=opt3b;
proc compare base=desired_output compare=opt4a;
proc compare base=desired_output compare=opt4b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 20:14:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-only-certain-observations/m-p/616033#M18844</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-01-08T20:14:02Z</dc:date>
    </item>
  </channel>
</rss>

