<?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 How to Create the Variable and Create an alert in the Log based on the Data condition? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831612#M328637</link>
    <description>&lt;P&gt;Sorry If my title is little confused. Not sure how to paraphrase it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my situation in my data. I am looking to find a way to create a variable and an alert based on the conditions I want. I can use the&amp;nbsp; 'IF' conditions . But it looks not efficient. Is there any way I can achieve this.&lt;/P&gt;
&lt;P&gt;I have a dataset with&amp;nbsp; two flags fl1,&amp;nbsp; fl2 and&amp;nbsp; 3 numeric scores x1, y1 and z1. But my data ( Id=104) have the errors based on following conditions.&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Condition 1: if&amp;nbsp; (fl1='Y' and fl2= 'Y')&amp;nbsp; or&amp;nbsp; (fl1='Y' and fl2= 'N')&amp;nbsp; or (fl1='N' and fl2= 'Y') then at least one score should be available.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Condition 2: If&amp;nbsp; (fl1='N' and fl2= 'N') then&amp;nbsp; x1, y1 and&amp;nbsp; z1 should not have any errors.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;Based on the above condition I need to create a Variable 'Result' based on it, Also looking for an Alert to show in the 'LOG' which ID have the Result='wrong';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#333399"&gt;&lt;STRONG&gt;Data have&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASuserlot_2-1662147373883.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74970i6A01B1E94EDAF55B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASuserlot_2-1662147373883.png" alt="SASuserlot_2-1662147373883.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#333399"&gt;&lt;STRONG&gt;Data How I want&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASuserlot_3-1662147413521.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74971iB8D6713534B4174B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASuserlot_3-1662147413521.png" alt="SASuserlot_3-1662147413521.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: for Example purpose I give 3 variables but my data have 15 numeric scores.&lt;/P&gt;
&lt;P&gt;Thank you for your time.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
 input id fl1$ fl2$ x1 y1 z1 ;
  cards;
 101 Y Y 1 1 6
 102 Y N 1 4 1
 103 N Y 1 1 3
 104 N N 1 1 1 
 105 N N . . .
 106 Y N 1 . 2
 107 N Y 3 . 1
 108 Y Y . . .
 ;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 02 Sep 2022 19:38:22 GMT</pubDate>
    <dc:creator>SASuserlot</dc:creator>
    <dc:date>2022-09-02T19:38:22Z</dc:date>
    <item>
      <title>How to Create the Variable and Create an alert in the Log based on the Data condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831612#M328637</link>
      <description>&lt;P&gt;Sorry If my title is little confused. Not sure how to paraphrase it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my situation in my data. I am looking to find a way to create a variable and an alert based on the conditions I want. I can use the&amp;nbsp; 'IF' conditions . But it looks not efficient. Is there any way I can achieve this.&lt;/P&gt;
&lt;P&gt;I have a dataset with&amp;nbsp; two flags fl1,&amp;nbsp; fl2 and&amp;nbsp; 3 numeric scores x1, y1 and z1. But my data ( Id=104) have the errors based on following conditions.&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Condition 1: if&amp;nbsp; (fl1='Y' and fl2= 'Y')&amp;nbsp; or&amp;nbsp; (fl1='Y' and fl2= 'N')&amp;nbsp; or (fl1='N' and fl2= 'Y') then at least one score should be available.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Condition 2: If&amp;nbsp; (fl1='N' and fl2= 'N') then&amp;nbsp; x1, y1 and&amp;nbsp; z1 should not have any errors.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#339966"&gt;Based on the above condition I need to create a Variable 'Result' based on it, Also looking for an Alert to show in the 'LOG' which ID have the Result='wrong';&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#333399"&gt;&lt;STRONG&gt;Data have&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASuserlot_2-1662147373883.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74970i6A01B1E94EDAF55B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASuserlot_2-1662147373883.png" alt="SASuserlot_2-1662147373883.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#333399"&gt;&lt;STRONG&gt;Data How I want&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASuserlot_3-1662147413521.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74971iB8D6713534B4174B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASuserlot_3-1662147413521.png" alt="SASuserlot_3-1662147413521.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: for Example purpose I give 3 variables but my data have 15 numeric scores.&lt;/P&gt;
&lt;P&gt;Thank you for your time.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
 input id fl1$ fl2$ x1 y1 z1 ;
  cards;
 101 Y Y 1 1 6
 102 Y N 1 4 1
 103 N Y 1 1 3
 104 N N 1 1 1 
 105 N N . . .
 106 Y N 1 . 2
 107 N Y 3 . 1
 108 Y Y . . .
 ;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Sep 2022 19:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831612#M328637</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-09-02T19:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create the Variable and Create an alert in the Log based on the Data condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831616#M328641</link>
      <description>&lt;P&gt;With: &lt;FONT color="#FF0000"&gt;Condition 1: if&amp;nbsp; (fl1='Y' and fl2= 'Y')&amp;nbsp; or&amp;nbsp; (fl1='Y' and fl2= 'N')&amp;nbsp; or (fl1='N' and fl2= 'Y') then at least one score should be available.&lt;/FONT&gt;&lt;FONT color="#000000"&gt; How is this to be used. It seems to be a statement, not an action or instruction to perform an action.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;From: &lt;FONT color="#FF0000"&gt;Condition 2: If&amp;nbsp; (fl1='N' and fl2= 'N') then&amp;nbsp; x1, y1 and&amp;nbsp; z1 should not have any errors.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;How do we know if x1, y1 or z1 have an error?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;When processing something with a data step you can use PUT statements to write to the log.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Depending on your environment if your message starts with "WARNING" or "ERROR" the text in the log may have a special appearance.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Run this code and look in the Log for appearance:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;data _null_;
   set sashelp.class;
   if age=11 then put "WARNING: Age is 11 for " name=;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Note that if you use numeric 1/0 codes for "flags" like that instead of 'Y' 'N' or similar character values your SAS code can get quite a bit easier to write. SAS will treat 0 or missing numeric values as "False" or "No" or what have you and other numbers as 'True' or 'Yes'.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;With multiple "flags" you can often do comparisons with numeric functions.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;If you want to know if any of the flags is true (1) then&amp;nbsp;&amp;nbsp; if max(flag1,flag2,flag3, ...) = 1.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;You can tell if all the flags are the same with the Range function, which would return 0 when all are the same.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;You can count how many "yes" values with sum: Countyes = sum(flag1, flag2, flag3, ...);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Percent of "yes" with: Percentyes = mean(flag1,flag2,flag3, ...);&amp;nbsp; The mean of a 1/0 coded variable is a decimal percentage: .33333 = 33.33 percent. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 19:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831616#M328641</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-02T19:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create the Variable and Create an alert in the Log based on the Data condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831617#M328642</link>
      <description>&lt;P&gt;Sorry for confusion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Yes those are instructions. Not conditions. Sorry for confusion.&lt;/P&gt;
&lt;P&gt;2. Based on fl1 fl2 flags. for ID=104 should have the missing values because fl1 and fl2 have 'N'. This is like gatekeeping question to enter the scores. x1, y1, z1 only have scores when one of the flag have the 'Y'&lt;/P&gt;
&lt;P&gt;3. It worked thanks for the Log alert.&lt;/P&gt;
&lt;P&gt;4. We/ You&amp;nbsp; are free to change type of data. I given flags (fl1, fl2) as characters , so that it will be easy to explain the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 20:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831617#M328642</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-09-02T20:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create the Variable and Create an alert in the Log based on the Data condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831618#M328643</link>
      <description>&lt;P&gt;Sorry for confusion.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Yes those are instructions. Not conditions. Sorry for confusion.&lt;/P&gt;
&lt;P&gt;2. Based on fl1 fl2 flags. for ID=104 should have the missing values because fl1 and fl2 have 'N'. This is like gatekeeping question to enter the scores. x1, y1, z1 only have scores when one of the flag have the 'Y'&lt;/P&gt;
&lt;P&gt;3. It worked thanks for the Log alert.&lt;/P&gt;
&lt;P&gt;4. We/ You&amp;nbsp; are free to change type of data. I given flags (fl1, fl2) as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 20:19:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831618#M328643</guid>
      <dc:creator>SASuserlot</dc:creator>
      <dc:date>2022-09-02T20:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to Create the Variable and Create an alert in the Log based on the Data condition?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831620#M328645</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This sort of check is known as an assertion, and is common in other languages.&amp;nbsp; &amp;nbsp;Many languages have an ASSERT() statement. The idea is that you write an expression which you expect to to be TRUE, and if it turns out to be FALSE, you want an error in the log, and you want to create a new variable that will show you which records failed the assertion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While SAS doesn't have an ASSERT statement, you can use a macro to make your own.&amp;nbsp; I wrote a paper where I presented an %ASSERT macro:&amp;nbsp;&lt;A href="https://www.lexjansen.com/nesug/nesug12/cc/cc31.pdf" target="_blank" rel="noopener"&gt;https://www.lexjansen.com/nesug/nesug12/cc/cc31.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The most basic assert macro is just:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro assert(assertion);
  if NOT (&amp;amp;assertion) then putlog "ERROR: Assertion (%superq(assertion)) is FALSE.";
%mend assert; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;With that, you can do simple assertions like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  %assert(fl1 IN ('Y' 'N')) /*assert fl1 is always within expected range*/
  %assert(fl2 IN ('Y' 'N'))
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When I read your logic, I see it as a single assertion.&amp;nbsp; I think your logic is: X1, Y1, and Z1 should all be missing, if and only if Fl1='N' and FL2='N' . So it's like a skip-pattern. Because of the way SAS does boolean logic, you can write that assertion as a single expression:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  %assert(  (fl1='N' and fl2='N') = (n(x1,y1,z1)=0)  )
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That assertion is checking that if the left side of the expression is true, then the right side must be true, and also checks if the left side is false, then the right side must be false.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the paper I have a longer version of the macro, which adds a parameter to write a custom error message, and a parameter to create a new RESULT variable like you want.&amp;nbsp; With that macro, you can code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id fl1$ fl2$ x1 y1 z1 ;
  cards;
 101 Y Y 1 1 6
 102 Y N 1 4 1
 103 N Y 1 1 3
 104 N N 1 1 1 
 105 N N . . .
 106 Y N 1 . 2
 107 N Y 3 . 1
 108 Y Y . . .
 ;
 run;

data want ;
  set have ;

  %assert(fl1 IN ('Y' 'N'))
  %assert(fl2 IN ('Y' 'N'))

  %assert( (fl1='N' and fl2='N') = (n(x1,y1,z1)=0)
         ,msg="Mismatch between flags and scores: " (id fl1 fl2 x1 y1 z1)(=)
         ,errorflag=Result
         )
run ;
   
proc print data=want ;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the log will flag two records as failing the assertion:&lt;/P&gt;
&lt;PRE&gt;15   data want ;
16     set have ;
17
18     %assert(fl1 IN ('Y' 'N'))
19     %assert(fl2 IN ('Y' 'N'))
20
21     %assert( (fl1='N' and fl2='N') = (n(x1,y1,z1)=0)
22            ,msg="Mismatch between flags and scores: " (id fl1 fl2 x1 y1 z1)(=)
23            ,errflag=Result
24            )
25   run ;

ERROR: USER ((fl1='N' and fl2='N') = (n(x1,y1,z1)=0)) is FALSE. Mismatch between flags and scores:
id=104 fl1=N fl2=N x1=1 y1=1 z1=1
ERROR: USER ((fl1='N' and fl2='N') = (n(x1,y1,z1)=0)) is FALSE. Mismatch between flags and scores:
id=108 fl1=Y fl2=Y x1=. y1=. z1=.
NOTE: There were 8 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 8 observations and 7 variables.
&lt;/PRE&gt;
&lt;P&gt;And the RESULT variable will show will flag them as well:&lt;/P&gt;
&lt;PRE&gt;Obs     id    fl1    fl2    x1    y1    z1    Result

 1     101     Y      Y      1     1     6       0
 2     102     Y      N      1     4     1       0
 3     103     N      Y      1     1     3       0
 4     104     N      N      1     1     1       1
 5     105     N      N      .     .     .       0
 6     106     Y      N      1     .     2       0
 7     107     N      Y      3     .     1       0
 8     108     Y      Y      .     .     .       1
&lt;/PRE&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;--Q.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 22:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Create-the-Variable-and-Create-an-alert-in-the-Log-based/m-p/831620#M328645</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-09-02T22:05:39Z</dc:date>
    </item>
  </channel>
</rss>

