<?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: Check if a row exists in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743372#M232729</link>
    <description>&lt;P&gt;Assuming 24M is the LAST visit you could take advantage of that fact.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  visit2=visit;
  if last.id then visit2='24M';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But that assumes the data is sorted by ID and VISIT, but your values of VISIT will not actually sort properly.&amp;nbsp; Do you have another variable that can be used to sort?&amp;nbsp; Perhaps numeric variable? Or a DATE variable?&lt;/P&gt;</description>
    <pubDate>Mon, 24 May 2021 13:48:30 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-24T13:48:30Z</dc:date>
    <item>
      <title>Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743314#M232698</link>
      <description>&lt;P&gt;Hi everyone, Thank you in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset similar to:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PtID&amp;nbsp; visit&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4M&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 12M&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24M&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8M&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12M&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each patient has multiple visit data. I want to set a condition if the visit= '24M' observation doesn't exist for a PtID. Eg: In this dataset, PtID 2 does not have a 24M visit. How would I create a flag for this, or set a condition based on the 24M visit not existing for this patient?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 08:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743314#M232698</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2021-05-24T08:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743327#M232704</link>
      <description>&lt;PRE&gt;DATA A;
	INFILE CARDS DLM=',' TRUNCOVER;
	INPUT @1 PTID :8. VISIT :$4.;
	CARDS;
1,4M
1,8M
1,16M
1,24M
2,24M
3,8M
4,128M
5,48M
5,24M
 ;
RUN;

PROC TRANSPOSE DATA=A OUT=AT;
	BY PTID;
	VAR VISIT;

PROC SQL NOPRINT;
	SELECT MAX(COUNT_PTID) FORMAT=Z8. INTO :NC FROM (SELECT COUNT(PTID) AS 
		COUNT_PTID, PTID FROM A GROUP BY PTID);

DATA C(KEEP=PTID);
	SET AT;
	ARRAY ARAT {&amp;amp;NC} COL1-COL&amp;amp;NC;

	DO I=1 TO &amp;amp;NC;

		IF ARAT[I]='24M' THEN
			OUTPUT;
	END;
RUN;

PROC PRINT DATA=C;&lt;/PRE&gt;
&lt;P&gt;Dataset C contains all PTID values containing 24M&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 10:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743327#M232704</guid>
      <dc:creator>nrk1787db1</dc:creator>
      <dc:date>2021-05-24T10:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743330#M232707</link>
      <description>&lt;P&gt;Where do you want to set the flag?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To simply get one observation per ptid, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if 0 then set have; /* only to keep variable order */
flag = 0;
do until (last.ptid);
  set have;
  by ptid;
  if visit = '24M' then flag = 1;
end;
keep ptid flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 May 2021 10:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743330#M232707</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-24T10:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743368#M232728</link>
      <description>&lt;P&gt;Hi Kurt,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;You are correct. There is no place to add a flag. Let me rephrase my question here:&lt;/P&gt;&lt;P&gt;Dataset I have:&lt;/P&gt;&lt;P&gt;PtID&amp;nbsp; visit&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4M&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 12M&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24M&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8M&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12M&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset I would like to create:&lt;/P&gt;&lt;P&gt;PtID&amp;nbsp; visit&amp;nbsp; &amp;nbsp;visit2&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4M&amp;nbsp; &amp;nbsp; &amp;nbsp;4M&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; 12M&amp;nbsp; &amp;nbsp; &amp;nbsp;12M&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;24M&amp;nbsp; &amp;nbsp; &amp;nbsp;24M&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8M&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8M&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;12M&amp;nbsp; &amp;nbsp; &amp;nbsp; 24M&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new variable 'visit2'. Visit2=visit, except if the 24M visit is missing. When it is missing, the 12M visit will be recoded to '24M' in the visit2 variable. Does this make sense?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 13:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743368#M232728</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2021-05-24T13:24:24Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743372#M232729</link>
      <description>&lt;P&gt;Assuming 24M is the LAST visit you could take advantage of that fact.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  visit2=visit;
  if last.id then visit2='24M';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But that assumes the data is sorted by ID and VISIT, but your values of VISIT will not actually sort properly.&amp;nbsp; Do you have another variable that can be used to sort?&amp;nbsp; Perhaps numeric variable? Or a DATE variable?&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 13:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743372#M232729</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-24T13:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743406#M232747</link>
      <description>Hi Tom, Thanks so much for the reply. However, 24M is not the last visit</description>
      <pubDate>Mon, 24 May 2021 17:24:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743406#M232747</guid>
      <dc:creator>Abishekaa</dc:creator>
      <dc:date>2021-05-24T17:24:30Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743420#M232759</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/102730"&gt;@Abishekaa&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;In your question , do you expect that every PID will have a 24M visit?&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 19:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743420#M232759</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-05-24T19:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: Check if a row exists in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743427#M232764</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/102730"&gt;@Abishekaa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi Tom, Thanks so much for the reply. However, 24M is not the last visit&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So I have no idea what your data looks like then.&amp;nbsp; Perhaps you can provide a more complete example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you just want to flag whether or not a particular visit exists then you might be able to just do that with dataset options.&amp;nbsp; So if you existing dataset is named HAVE and the two variables of interest are named PTID and VISIT here is a data step that will create a new variable named HAS24M to indicate if the PTID has a VISIT '24M' or not.&amp;nbsp; Note this assumes the data is sorted by PTID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have
        have(in=in2 keep=ptid visit rename=(visit=visit24m) where=('24M'=visit24m))
  ;
  by ptid ;
  has24m = in2 ;
  drop visit24m ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 May 2021 20:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-a-row-exists-in-a-dataset/m-p/743427#M232764</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-24T20:02:32Z</dc:date>
    </item>
  </channel>
</rss>

