<?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: How can I subset a dataset according to the value of a variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783784#M250006</link>
    <description>&lt;P&gt;Not sure if I understood your requirement. But, for what it is worth -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
	array k[10] $ k1-k10;
	do _n_=1 by 1 until(last.id);
		set test;
		by id;
		k[_n_]=type;
	end;

		st=whichc('xin',of k1-k10);

	do _n_=1 to _n_ ;
		set test;	
		if visit &amp;gt;= st	then do;
			if type in ('xin','') then output;
		end;
	end;

	drop k: st;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 02 Dec 2021 21:12:33 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2021-12-02T21:12:33Z</dc:date>
    <item>
      <title>How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783701#M249970</link>
      <description>&lt;P&gt;Hi Friends, I want to subset my dataset when the type variable starts from Xin and ends with Xin or blank value. How should I write the code? Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test; 
input id visit type $ ; 
datalines; 
1 1 xin 
1 2 . 
1 3 xin 
2 1 other 
2 2 xin 
2 3 . 
2 4 other 
3 1 . 
3 2 xin 
3 3 . 
3 4 .
 ; 
run; &lt;BR /&gt;
data want; 
input id visit type $ ; 
datalines; 
1 1 xin 
1 2 . 
1 3 xin 
2 2 xin 
2 3 . 
3 2 xin 
3 3 . 
3 4 . 
; &lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 17:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783701#M249970</guid>
      <dc:creator>superbibi</dc:creator>
      <dc:date>2021-12-02T17:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783706#M249975</link>
      <description>&lt;P&gt;Not certain I understand your request fully. You seem to indicate you want all observations between and including the obs where type="xin", but then for ID=2 you don't pick up observation 2 4 other, yet there is an opening xin (2 2 xin) and no closing xin.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ; 
	input id visit type $ ; 
datalines; 
1 1 xin 
1 2 . 
1 3 xin 
2 1 other 
2 2 xin 
2 3 . 
2 4 other 
3 1 . 
3 2 xin 
3 3 . 
3 4 .
 ; 
run; 

data want ;
	drop flag ;
	retain flag 0 ;
	set have ;
	by id ;
	/* reset flag if this is the first occurrence of ID */
	if first.id then
		flag=0 ;
	/* if type="xin" flip flag (0|1) and output */
	if type="xin" then do ;
		flag=mod(flag+1,2) ;
		output ;
	end ;
	/* if type ne "xin" and flag=1 and output */
	else if flag=1 then 
		output ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 17:29:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783706#M249975</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2021-12-02T17:29:52Z</dc:date>
    </item>
    <item>
      <title>Re: How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783708#M249976</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/117839"&gt;@superbibi&lt;/a&gt;, I'm not sure that I fully understand your requirement "&lt;SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;starts from&lt;/STRONG&gt; &lt;/EM&gt;Xin and &lt;EM&gt;&lt;STRONG&gt;ends with&lt;/STRONG&gt; &lt;/EM&gt;Xin&lt;/SPAN&gt;".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have assumed you only want a type of "xin" (lower case) or missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
input id visit type : $ ; 
datalines; 
1 1 xin 
1 2 . 
1 3 xin 
2 1 other 
2 2 xin 
2 3 . 
2 4 other 
3 1 . 
3 2 xin 
3 3 . 
3 4 .
; 
run; 


data want;
   set test;
   where missing(type) or type eq 'xin';
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;Kind regards,&lt;/P&gt;
&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Dec 2021 17:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783708#M249976</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2021-12-02T17:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783784#M250006</link>
      <description>&lt;P&gt;Not sure if I understood your requirement. But, for what it is worth -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
	array k[10] $ k1-k10;
	do _n_=1 by 1 until(last.id);
		set test;
		by id;
		k[_n_]=type;
	end;

		st=whichc('xin',of k1-k10);

	do _n_=1 to _n_ ;
		set test;	
		if visit &amp;gt;= st	then do;
			if type in ('xin','') then output;
		end;
	end;

	drop k: st;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Dec 2021 21:12:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783784#M250006</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-12-02T21:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783814#M250021</link>
      <description>&lt;P&gt;Thank you very much for the help. It works well in this sample dataset, I will see if it goes the same in the full dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Dec 2021 02:07:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783814#M250021</guid>
      <dc:creator>superbibi</dc:creator>
      <dc:date>2021-12-03T02:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783909#M250074</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
input id visit type $ ; 
datalines; 
1 1 xin 
1 2 . 
1 3 xin 
2 1 other 
2 2 xin 
2 3 . 
2 4 other 
3 1 . 
3 2 xin 
3 3 . 
3 4 .
 ; 
run; 


data temp;
 set test;
 by id;
 n+1;
 if type='xin' or  missing(type);
run;
data temp1;
 set temp;
 by id;
 if type='xin' or  (last.id and missing(type));
run;
data temp2;
 set temp1;
 by id;
 if first.id then seq=0;
 seq+1;
 if mod(seq,2)=1 then group+1;
run;
data temp3;
 merge temp2 temp2(keep=group n rename=(group=_group n=_n) firstobs=2);
 output;
 if group=_group then do;
   do i=n+1 to _n-1;
    n=i;output;
   end;
 end;
keep n;
run;

data want;
 if _n_=1 then do;
  if 0 then set temp3;
  declare hash h(dataset:'temp3');
  h.definekey('n');
  h.definedone();
 end;
set test;
if h.check(key:_n_)=0;
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Dec 2021 12:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/783909#M250074</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-12-03T12:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I subset a dataset according to the value of a variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/784323#M250283</link>
      <description>&lt;P&gt;Thank you again for the solution. I just realized there is another situation for id2, the "type" variable might have "other xin blank other blank&amp;nbsp; " sequence and I do not want any records after the second "other" as shown in the new code. It means for id 2, visit before the first "xin" and all visits equal to or larger than 4 are supposed to be dropped. What is your suggestion? Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data test;
input id  visit   type $   ;
datalines;
1 1 xin
1 2 .
1 3 xin
2 1 other
2 2 xin
2 3 .
2 4 other
2 5 .
3 1 .
3 2 xin
3 3 .
3 4 .
;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data&amp;nbsp;want;&lt;BR /&gt;input id  visit   type $   ;
datalines;
1 1 xin
1 2 .
1 3 xin

2 2 xin
2 3 .&lt;BR /&gt;
3 2 xin
3 3 .
3 4 .
;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;"&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Dec 2021 15:01:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-subset-a-dataset-according-to-the-value-of-a-variable/m-p/784323#M250283</guid>
      <dc:creator>superbibi</dc:creator>
      <dc:date>2021-12-06T15:01:34Z</dc:date>
    </item>
  </channel>
</rss>

