<?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 Re: Set values to missing if less than 10 records in any of two classes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910605#M359101</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459961"&gt;@Frk_bolsen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;By number of records I mean number of non-missing records.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Records cannot be missing. Perhaps you mean number of records with non-missing value for BOTH yield and cellcount? What about those records where either yield is missing or cellcount is missing, but they are not both missing, how are those counted?&lt;/P&gt;</description>
    <pubDate>Fri, 05 Jan 2024 13:30:50 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2024-01-05T13:30:50Z</dc:date>
    <item>
      <title>Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910589#M359092</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I am working with a larger version of the dataset shown below:&lt;/SPAN&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;yield&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;&lt;SPAN&gt;cellcount&lt;/SPAN&gt;&lt;/ST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;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I want to set yield and cellcount to missing if the htd or hya have less than 10 records. Edits in htd will affect hya, and the other way around. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I use the code below and repeat this a number of times so that I am sure that all htd’s and hya’s have minimum 10 records each in the end. Can you help me with a loop, or something like that, that does this automatically and stops when all htd’s and hya’s have at least 10 records?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Title 'Minimum 10 records per htd - first test';
proc means noprint n data=test maxdec=3;
class htd;
var yield cellcount;
output out=htd_n
n=yieldn cellcountn;
run;

proc sort data=htd_n;	by htd;	run;
proc sort data=test;	by htd;	run;

data test;
merge test (in=a) htd_n;
by htd;
if a=1;
    if yieldn&amp;lt;10 then yield=.;
	if cellcountn&amp;lt;10 then cellcount=.;
	drop _TYPE_ _FREQ_;
run;
proc means data=test ;
	var yield cellcount;
run;

Title 'Minimum 10 records per hya – first test';
proc means noprint n data=test maxdec=3;
class hya;
var yield cellcount;
output out=hya_n
n=yieldn cellcountn;
run;

proc sort data=test;	by hya;	run;
proc sort data=hya_n;	by hya;	run;

data test;
merge test (in=a) hya_n;
by hya;
if a=1;
    if yieldn&amp;lt;10 then yield=.;
	if cellcountn&amp;lt;10 then cellcount=.;
	drop _TYPE_ _FREQ_;
run;
proc means data=test;
	var yield cellcount;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 11:31:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910589#M359092</guid>
      <dc:creator>Frk_bolsen</dc:creator>
      <dc:date>2024-01-05T11:31:03Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910591#M359094</link>
      <description>&lt;P&gt;Some clarification would be helpful. Your words are:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I want to set yield and cellcount to missing if the htd or hya have less than 10 records. Edits in htd will affect hya, and the other way around.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I use the code below and repeat this a number of times so that I am sure that all htd’s and hya’s have minimum 10 records each in the end. Can you help me with a loop, or something like that, that does this automatically and stops when all htd’s and hya’s have at least 10 records?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The part that really confuses me is "that does this automatically and stops when all htd’s and hya’s have at least 10 records". I am also very unclear about this part: "Edits in htd will affect hya, and the other way around" as it doesn't seem to fit in with the rest of the problem.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Does this mean:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;You are working with data that has some htd/hya combinations that have &amp;lt;10 records and some that have &amp;gt;=10 records, and you want to set cellcount and yield to missing if the count for the htd/hya combination is &amp;lt;10; OR&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;You are deleting records such that what remains is only htd/hya combinations that have &amp;gt;=10 records; OR&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;You are adding records such that what every htd/hya combinations have &amp;gt;=10 records; OR&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;something else?&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 05 Jan 2024 12:13:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910591#M359094</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-05T12:13:23Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910595#M359095</link>
      <description>&lt;P&gt;Sorry about the confusion. i will try to clarify.&lt;/P&gt;&lt;P&gt;In the first htd test, I check number of records of yield and cellcount in all classes&amp;nbsp;of htd. If any htd have less than 10 records, I set all yield and/or cellcount values to missing in that group.&lt;/P&gt;&lt;P&gt;Then I do the same for hya, but doing this will affect the number of records in htd, meaning that I have to repeat these steps a number of times to end up with a dataset that has at least 10 records of both htd and hya. The code I put here, is repeated 5 times in my program, but I didnt show that here.&lt;/P&gt;&lt;P&gt;I am thinking that it has to be a better way of doing this, having sas repeating these steps for me and stopping when the number of records are above 10 for yield and cellcount in both htd and hya.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 12:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910595#M359095</guid>
      <dc:creator>Frk_bolsen</dc:creator>
      <dc:date>2024-01-05T12:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910597#M359097</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;In the first htd test, I check number of records of yield and cellcount in all classes&amp;nbsp;of htd. If any htd have less than 10 records, I set all yield and/or cellcount values to missing in that group.&lt;/P&gt;
&lt;P&gt;Then I do the same for yha, but doing this will affect the number of records in htd&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Not following this either. If there were five records for a htd level, and you perform this activity of setting missing value of yield and cell count to this htd level, there are still the same number of records for each yha level. The number of records doesn't change when you set yield and cell count to missing.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 12:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910597#M359097</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-05T12:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910599#M359098</link>
      <description>&lt;P&gt;Yes, they are depended on each other. By number of records I mean number of non-missing records. Excluding records of htd will affect some hya's and the other way around. I understand I have to come back with a better explanation, but that has to be at a later time.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 13:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910599#M359098</guid>
      <dc:creator>Frk_bolsen</dc:creator>
      <dc:date>2024-01-05T13:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910605#M359101</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459961"&gt;@Frk_bolsen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;By number of records I mean number of non-missing records.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Records cannot be missing. Perhaps you mean number of records with non-missing value for BOTH yield and cellcount? What about those records where either yield is missing or cellcount is missing, but they are not both missing, how are those counted?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 13:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910605#M359101</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-01-05T13:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910654#M359111</link>
      <description>&lt;P&gt;Suggestion: Change your rules for demonstration purposes to something smaller than 10, say 3 or 4.&lt;/P&gt;
&lt;P&gt;Then provide a working data step to create source such that a couple of your rule values are met and few where not met.&lt;/P&gt;
&lt;P&gt;Then show us a working data step of the desired result after your rules are implemented. NOT code to create the result, the actual result. You may have to point out which rule(s) were met or violated for some of the source records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The recommendation for a smaller count for the rule is to reduce possible complications when trying to match up larger number of observations.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jan 2024 18:23:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910654#M359111</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-01-05T18:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910727#M359132</link>
      <description>&lt;P&gt;Agree with all that has been said in regards of how to provide sample data, desired result etc.&lt;/P&gt;
&lt;P&gt;I tried to understand and work with what you've provided. Is below returning what you're after? And if not what needs change and why?&lt;/P&gt;
&lt;P&gt;I didn't overwrite variables but populated new ones so it's easier to follow what's happening.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.have;
  infile datalines truncover dsd;
  input yield cellcount htd hya;
  datalines;
2.2,260,1,11120101
.,110,1,11120101
5,90,1,11120101
2,400,1,11120101
3,560,1,11120101
4.5,90,2,11120101
2.2,60,2,11120101
3.4,.,2,11120101
1.2,.,2,11120101
1.6,280,2,11120101
;

proc sort data=work.have out=work.inter;
  by hya htd;
run;

proc means data=work.inter noprint n maxdec=3 nway;
  class hya htd;
  var yield cellcount;
  output 
    out=work.hya_htd_n
    n=yieldn cellcountn
    ;
run;

%let hya_htd_threshold=4;
%let hya_threshold    =8;

data  
   work.lev_hya_htd (keep=hya htd yieldn_hya_htd cellcountn_hya_htd)
   work.lev_hya     (keep=hya     yieldn_hya cellcountn_hya)
  ;
  set work.hya_htd_n;
  by hya htd;
  
  if yieldn     &amp;gt;= &amp;amp;hya_htd_threshold then yieldn_hya_htd     =yieldn;
  if cellcountn &amp;gt;= &amp;amp;hya_htd_threshold then cellcountn_hya_htd =cellcountn;
  output work.lev_hya_htd;

  yieldn_hya+yieldn_hya_htd;
  cellcountn_hya+cellcountn_hya_htd;

  if last.hya then
    do;
      if yieldn_hya     &amp;lt; &amp;amp;hya_threshold then call missing(yieldn_hya);
      if cellcountn_hya &amp;lt; &amp;amp;hya_threshold then call missing(cellcountn_hya);
      output work.lev_hya;
      call missing(yieldn_hya, cellcountn_hya);
    end;
run;

data work.want;
  merge work.inter work.lev_hya_htd;
  by hya htd;
  if _n_=1 then
    do;
      if 0 then set work.lev_hya(keep=yieldn_hya cellcountn_hya);
      dcl hash h1(dataset:'work.lev_hya');
      h1.defineKey('hya');
      h1.defineData('yieldn_hya','cellcountn_hya');
      h1.defineDone();
    end;
  if h1.find() ne 0 then call missing(yieldn_hya,cellcountn_hya);
run;

proc print data=want;
run;

proc print data=lev_hya;
run;
proc print data=lev_hya_htd;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1704516548249.png" style="width: 544px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92341i55E9C8BE07C42ED3/image-dimensions/544x407?v=v2" width="544" height="407" role="button" title="Patrick_0-1704516548249.png" alt="Patrick_0-1704516548249.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>Sat, 06 Jan 2024 04:49:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/910727#M359132</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-01-06T04:49:17Z</dc:date>
    </item>
    <item>
      <title>Re: Set values to missing if less than 10 records in any of two classes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/911024#M359243</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/459961"&gt;@Frk_bolsen&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your question reminded me of a thread that I contributed a &lt;A href="https://communities.sas.com/t5/SAS-Programming/Combining-rows-of-data/m-p/259062/highlight/true#M50047" target="_blank" rel="noopener"&gt;solution&lt;/A&gt; to in 2016. So let me suggest a somewhat similar approach using a SAS macro which iteratively alternates between two "elimination" tasks until a stopping condition is met.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample data for demonstration */

data have;
call streaminit(27182818);
do _n_=1 to 1000;
  hya=11120100+rand('integer',60);
  htd=rand('integer',60);
  yield=ifn(rand('bern',0.83),round(rand('uniform',9),0.1),.);
  cellcount=ifn(rand('bern',0.85),round(rand('integer',1000),10),.);
  output;
end;
run;

proc sort data=have;
by hya htd;
run;

/* Add a temporary unique key _SEQNO */

data _tmp / view=_tmp;
set have;
by hya htd;
_seqno=_n_;
run;

/* Iteratively eliminate HYA and HTD groups with fewer than 10 non-missing &amp;amp;VAR values
   by setting their &amp;amp;VAR values to missing */

%macro elim(data=, clvar1=, clvar2=, var=, need=10, out=);
%local i j empty min1 min2;

%* Initial count of non-missing &amp;amp;VAR values;

proc summary data=&amp;amp;data;
class &amp;amp;clvar1 &amp;amp;clvar2;
var &amp;amp;var;
types &amp;amp;clvar1 &amp;amp;clvar2 &amp;amp;clvar1*&amp;amp;clvar2;
output out=_cnt(drop=_freq_) n=;
run;

%* Check if any groups need to be eliminated;

proc sql noprint;
select min(&amp;amp;var) into :min1-
from _cnt
where _type_&amp;lt;3
group by _type_
order by _type_ desc;  /* _type_=1 corresponds to &amp;amp;clvar2, */
quit;                  /* _type_=2 corresponds to &amp;amp;clvar1. */

%if &amp;amp;min1&amp;lt;&amp;amp;need | &amp;amp;min2&amp;lt;&amp;amp;need %then %do;

  %if &amp;amp;min1&amp;lt;&amp;amp;min2 %then %let i=1;
  %else %let i=2;

  %do %until(&amp;amp;&amp;amp;min&amp;amp;i&amp;gt;=&amp;amp;need | &amp;amp;empty);
    %let j=%eval(3-&amp;amp;i);

    %* Elimination step;

    proc sql;
    create table _cnt1 as
    select a.* from _cnt(where=(_type_=3)) a
               join _cnt(where=(_type_=&amp;amp;j &amp;amp; &amp;amp;var&amp;gt;=&amp;amp;need)) b
    on a.&amp;amp;&amp;amp;clvar&amp;amp;i=b.&amp;amp;&amp;amp;clvar&amp;amp;i;
    quit;

    %let empty=%eval(&amp;amp;sqlobs=0);

    %* Count non-missing &amp;amp;VAR values after elimination;

    proc summary data=_cnt1;
    class &amp;amp;clvar1 &amp;amp;clvar2;
    var &amp;amp;var;
    freq &amp;amp;var;
    types &amp;amp;&amp;amp;clvar&amp;amp;j &amp;amp;clvar1*&amp;amp;clvar2;
    output out=_cnt(drop=_freq_) n=;
    run;

    %* Check if more groups need to be eliminated;

    proc sql noprint;
    select min(&amp;amp;var) into :min&amp;amp;i trimmed
    from _cnt
    where _type_&amp;lt;3;
    quit;

    %let i=&amp;amp;j;
  %end;

  %* Create output dataset with &amp;amp;VAR values set to missing in eliminated groups;

  proc sql;
  create table &amp;amp;out as
  select a.&amp;amp;clvar1, a.&amp;amp;clvar2, a._seqno,
         case when b.&amp;amp;clvar1=. then . else &amp;amp;var end as &amp;amp;var
  from _tmp a left join _cnt1(drop=&amp;amp;var) b
  on a.&amp;amp;clvar1=b.&amp;amp;clvar1 &amp;amp; a.&amp;amp;clvar2=b.&amp;amp;clvar2
  order by _seqno;
  quit;

  %if &amp;amp;empty %then %put WAR%str(NING): All %upcase(&amp;amp;var) values have been set to missing!;

%end;

%else %do;
  %put All %upcase(&amp;amp;clvar1) and %upcase(&amp;amp;clvar2) groups in dataset %upcase(&amp;amp;data) have &amp;gt;=%upcase(&amp;amp;need) non-missing %upcase(&amp;amp;var) values.;
  data &amp;amp;out;
  set _tmp(keep=&amp;amp;clvar1 &amp;amp;clvar2 &amp;amp;var _seqno);
  run;
%end;
%mend elim;

%elim(data=have, clvar1=hya, clvar2=htd, var=yield,     out=out_y)
%elim(data=have, clvar1=hya, clvar2=htd, var=cellcount, out=out_c)

/* Combine results for YIELD and CELLCOUNT */

data want(drop=_seqno);
merge out_y out_c(drop=hya htd);
by _seqno;
run;

/* Optional: Verify that all HYA and HTD groups with non-missing YIELD values have &amp;gt;=10 of those */

proc summary data=want;
where yield&amp;gt;.z;
class hya htd;
ways 1;
var yield;
output out=stats_y(drop=_freq_) n=n_yield;
run;

proc print data=stats_y;
run;

/* Optional: Verify that all HYA and HTD groups with non-missing CELLCOUNT values have &amp;gt;=10 of those */

proc summary data=want;
where cellcount&amp;gt;.z;
class hya htd;
ways 1;
var cellcount;
output out=stats_c(drop=_freq_) n=n_cellcount;
run;

proc print data=stats_c;
run;

/* Optional: Select HYA and HTD groups with both &amp;gt;=10 non-missing YIELD values and &amp;gt;=10 non-missing CELLCOUNT values */

proc sql;
create table sel as
select * from want
where hya in (select hya from stats_y where _type_=2
                intersect
              select hya from stats_c where _type_=2)
  and htd in (select htd from stats_y where _type_=1
                intersect
              select htd from stats_c where _type_=1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Jan 2024 16:02:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Set-values-to-missing-if-less-than-10-records-in-any-of-two/m-p/911024#M359243</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-01-09T16:02:38Z</dc:date>
    </item>
  </channel>
</rss>

