<?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 Checking flag variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620692#M182425</link>
    <description>&lt;P&gt;I have the following sample data below:&lt;/P&gt;&lt;P&gt;data test_a;&lt;BR /&gt;infile datalines dlm= ',';&lt;BR /&gt;input id reg_date:date9. startdate:date9. enddate:date9. extdate:date9.;&lt;BR /&gt;format reg_date startdate enddate extdate date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;005,01JUL2017,31JUl2016,3JUL2017,31AUG2018&lt;BR /&gt;007,25NOV2017,31JUL2016,31JUL2017,31OCT2018&lt;BR /&gt;001,12MAR2018,31JAN2016,31JAN2017,30APR2018&lt;BR /&gt;002,05JUN2018,29FEB2016,28FEB2017,31MAY2018&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;and I create flag variables using the following code to get the dataset below.&lt;BR /&gt;Everything works fine but I have alot of data and I would like to know if there is a&lt;BR /&gt;code that can be run to ensure that only one category is being flagged per ID .&lt;BR /&gt;I know that if/then should only flag in one category (like it does below) but I would&lt;BR /&gt;like to do an additional check.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test_b;&lt;BR /&gt;set test_a;&lt;BR /&gt;if startdate le reg_date le enddate then withinyr = 1;&lt;BR /&gt;else if enddate le reg_date le extdate then withingrace = 1;&lt;BR /&gt;else outsidetimeframe = 1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obs id reg_date startdate enddate extdate withinyr withingrace outsidetimeframe&lt;BR /&gt;1 5 01JUL2017 31JUL2016 03JUL2017 31AUG2018 1 . .&lt;BR /&gt;2 7 25NOV2017 31JUL2016 31JUL2017 31OCT2018 . 1 .&lt;BR /&gt;3 1 12MAR2018 31JAN2016 31JAN2017 30APR2018 . 1 .&lt;BR /&gt;4 2 05JUN2018 29FEB2016 28FEB2017 31MAY2018 . . 1&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jan 2020 22:21:29 GMT</pubDate>
    <dc:creator>luvscandy27</dc:creator>
    <dc:date>2020-01-28T22:21:29Z</dc:date>
    <item>
      <title>Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620692#M182425</link>
      <description>&lt;P&gt;I have the following sample data below:&lt;/P&gt;&lt;P&gt;data test_a;&lt;BR /&gt;infile datalines dlm= ',';&lt;BR /&gt;input id reg_date:date9. startdate:date9. enddate:date9. extdate:date9.;&lt;BR /&gt;format reg_date startdate enddate extdate date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;005,01JUL2017,31JUl2016,3JUL2017,31AUG2018&lt;BR /&gt;007,25NOV2017,31JUL2016,31JUL2017,31OCT2018&lt;BR /&gt;001,12MAR2018,31JAN2016,31JAN2017,30APR2018&lt;BR /&gt;002,05JUN2018,29FEB2016,28FEB2017,31MAY2018&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;and I create flag variables using the following code to get the dataset below.&lt;BR /&gt;Everything works fine but I have alot of data and I would like to know if there is a&lt;BR /&gt;code that can be run to ensure that only one category is being flagged per ID .&lt;BR /&gt;I know that if/then should only flag in one category (like it does below) but I would&lt;BR /&gt;like to do an additional check.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test_b;&lt;BR /&gt;set test_a;&lt;BR /&gt;if startdate le reg_date le enddate then withinyr = 1;&lt;BR /&gt;else if enddate le reg_date le extdate then withingrace = 1;&lt;BR /&gt;else outsidetimeframe = 1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obs id reg_date startdate enddate extdate withinyr withingrace outsidetimeframe&lt;BR /&gt;1 5 01JUL2017 31JUL2016 03JUL2017 31AUG2018 1 . .&lt;BR /&gt;2 7 25NOV2017 31JUL2016 31JUL2017 31OCT2018 . 1 .&lt;BR /&gt;3 1 12MAR2018 31JAN2016 31JAN2017 30APR2018 . 1 .&lt;BR /&gt;4 2 05JUN2018 29FEB2016 28FEB2017 31MAY2018 . . 1&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 22:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620692#M182425</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-01-28T22:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620694#M182426</link>
      <description>&lt;P&gt;You could have another flag like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if sum(withinyr,withingrace, outsidetimeframe)&amp;gt;1 then flag='Alert';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is assuming i understand what you mean&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_b;
set test_a;
if startdate le reg_date le enddate then withinyr = 1;
else if enddate le reg_date le extdate then withingrace = 1;
else outsidetimeframe = 1;
if sum(withinyr,withingrace, outsidetimeframe)&amp;gt;1 then flag='Alert';
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jan 2020 22:30:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620694#M182426</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-28T22:30:22Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620698#M182428</link>
      <description>&lt;P&gt;I just wanna make sure that if a ID is counted in withinyr then it is not also counted as withingrace.&lt;BR /&gt;I just want to make sure that a id is not counted in more than one category.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 22:39:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620698#M182428</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-01-28T22:39:15Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620699#M182429</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You could have another flag like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if sum(withinyr,withingrace, outsidetimeframe)&amp;gt;1 then flag='Alert';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is assuming i understand what you mean&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_b;
set test_a;
if startdate le reg_date le enddate then withinyr = 1;
else if enddate le reg_date le extdate then withingrace = 1;
else outsidetimeframe = 1;
if sum(withinyr,withingrace, outsidetimeframe)&amp;gt;1 then flag='Alert';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/255656"&gt;@luvscandy27&lt;/a&gt;&amp;nbsp;shouldn't even assign values to more than one variable then it may be worth considering if more than one value was assigned to any of the group of variables.&lt;/P&gt;
&lt;PRE&gt;if N(withinyr,withingrace, outsidetimeframe)&amp;gt;1 then flag='Alert';&lt;/PRE&gt;
&lt;P&gt;or even not exactly = 1 meaning no flag was set (if you expect exactly one to be set for every record).&lt;/P&gt;
&lt;PRE&gt;if N(withinyr,withingrace, outsidetimeframe) ne 1 then flag='Alert';&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Jan 2020 22:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620699#M182429</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-01-28T22:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620704#M182432</link>
      <description>&lt;P&gt;Agreed. I got confused.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 22:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620704#M182432</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-28T22:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620706#M182434</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Agreed. I got confused.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Nothing wrong with your suggestion, just opening some thought lines about what&amp;nbsp;might actually&amp;nbsp;be needed. I'm not sure that OP is really sure yet.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 23:00:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620706#M182434</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-01-28T23:00:16Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620707#M182435</link>
      <description>&lt;P&gt;I may not be explaining this correctly I apologize. Looking at the example below I want to make sure&lt;BR /&gt;that for a id it only flags one category. If I have id 5(pictured below) and it flags in more&lt;BR /&gt;than one category then i would like to create a check that would alert me to that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;obs id&amp;nbsp; &amp;nbsp; &amp;nbsp;reg_date startdate&amp;nbsp; &amp;nbsp; &amp;nbsp; enddate&amp;nbsp; &amp;nbsp; extdate&amp;nbsp; &amp;nbsp; &amp;nbsp;withinyr withingrace outsidetimeframe&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; 7/1/2017 7/31/2016&amp;nbsp; &amp;nbsp;7/3/2017&amp;nbsp; &amp;nbsp;8/31/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 23:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620707#M182435</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2020-01-28T23:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620708#M182436</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/255656"&gt;@luvscandy27&lt;/a&gt;&amp;nbsp; Much better. Can you explain how you want to design the Flag , or in other words how you want it. Some will have PUT texts writing to LOG. Some will have flag variables with variable names captured, Some will have an audit data set and many others.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively wisdom comes from experience,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; might suggest something regarding the design&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 23:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620708#M182436</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-28T23:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620710#M182437</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/255656"&gt;@luvscandy27&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I may not be explaining this correctly I apologize. Looking at the example below I want to make sure&lt;BR /&gt;that for a id it only flags one category. If I have id 5(pictured below) and it flags in more&lt;BR /&gt;than one category then i would like to create a check that would alert me to that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;obs id&amp;nbsp; &amp;nbsp; &amp;nbsp;reg_date startdate&amp;nbsp; &amp;nbsp; &amp;nbsp; enddate&amp;nbsp; &amp;nbsp; extdate&amp;nbsp; &amp;nbsp; &amp;nbsp;withinyr withingrace outsidetimeframe&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; 7/1/2017 7/31/2016&amp;nbsp; &amp;nbsp;7/3/2017&amp;nbsp; &amp;nbsp;8/31/2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Any of the suggested SUM(&amp;lt;of your flag variables&amp;gt;) &amp;gt;1 or N (&amp;lt;of your flag variables&amp;gt;) discussed above will add a variable you could use to find any of those. Add the example line to the data step code that creates the flag variables AFTER they should be assigned.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To find those you can use another data step or report proc with a WHERE clause or statement:&lt;/P&gt;
&lt;P&gt;For example.&lt;/P&gt;
&lt;P&gt;proc print data=have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; where flag=&amp;lt;what ever value was set&amp;gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or if you read your log (you do don't you) then you could use:&lt;/P&gt;
&lt;P&gt;If N (withinyr, withingrace, outsidetimeframe) &amp;gt; 1 then put "WARNING: Multiple flag variables set for " id= +1 reg_date=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OR your need to be much more explicit about what you mean by "create a check".&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 23:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620710#M182437</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-01-28T23:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Checking flag variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620926#M182494</link>
      <description>&lt;P&gt;With your code, you can only get one flag per record. Unless something is seriously wrong with basic logic, or with the basic SAS datastep compiler, or with your computer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if your data contains multiple records for the same ID, and you want to find the IDs where different records have different flags, that's another story.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One solution could be this:&lt;/P&gt;
&lt;PRE&gt;proc sql;&lt;BR /&gt;  select * from test_b &lt;BR /&gt;  group by ID&lt;BR /&gt;  having sum(max(withinyr),max(withingrace),max(outsidetimeframe))&amp;gt;1;&lt;BR /&gt;quit;&lt;BR /&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2020 17:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Checking-flag-variables/m-p/620926#M182494</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-01-29T17:55:28Z</dc:date>
    </item>
  </channel>
</rss>

