<?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: Missing values were generated as a result of performing an operation on missing values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715661#M221076</link>
    <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; !&lt;BR /&gt;Thank you!&lt;BR /&gt; Can I ask what do column 7 and 11 mean?&lt;BR /&gt;Many thanks!</description>
    <pubDate>Sun, 31 Jan 2021 19:57:09 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-01-31T19:57:09Z</dc:date>
    <item>
      <title>Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715648#M221064</link>
      <description>&lt;P&gt;Hi SAS Users!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is a familiar topic but today I face it and do not know how to trace it.&lt;/P&gt;
&lt;P&gt;I have a couple of questions relating to my code as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data argentinalag;
    set argentina;
    by type;
	lags7=lag(s7);
    if first.type then lags7=.;
    lags3=lag(s3);
    if first.type then lags3=.;
    
	lags18=lag(s18);
    if first.type then lags18=.;

    *log(0) in meaningless so set these obs equalling to missing;
	if (1+s27/s2)&amp;gt;0 and (1+s27/s2) ne .  then do;
	cf_ope_act= log(1 + s27/s2);
    lagcf_ope_act=lag(cf_ope_act);
    if first.type then lagcf_ope_act=.;
	end;
	lags29 = lag(s29);
    if first.type then lags29=.;

	lags22 = lag(s22);
	if first.type then lags22=.;

	lags43 = lag(s43);
    if first.type then lags43=.;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the log is as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;63         	lags22 = lag(s22);
64         	if first.type then lags22=.;
65         
66         	lags43 = lag(s43);
67             if first.type then lags43=.;
68            run;

NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      238 at 52:7    238 at 52:11   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So, 2 questions are:&lt;/P&gt;
&lt;P&gt;1. Can I merge the code&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then lags3=. and lags18=. and lags29=.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or I must separate them as I did in my code&lt;/P&gt;
&lt;P&gt;2. How to trace the missing value as the log above? what does "Number of times" = 238 mean?&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715648#M221064</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-31T19:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715649#M221065</link>
      <description>&lt;P&gt;1) Yes. You can do this instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then call missing(lags3, lags18, lags29);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2) You perform an operation on missing values 238 times in your data step.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715649#M221065</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-31T19:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715650#M221066</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your answer, I try to find from the dataset Argentina but cannot find any mistake from line 52, column 7.&lt;/P&gt;
&lt;P&gt;I also attach the file Argentina here, can you please help me to spot it out ?&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715650#M221066</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-31T19:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715651#M221067</link>
      <description>&lt;P&gt;The&amp;nbsp;&lt;SPAN&gt;line 52, column 7 is line 52 in the SAS log. That is where you need to look &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Not the data.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715651#M221067</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-31T19:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715652#M221068</link>
      <description>&lt;OL&gt;
&lt;LI&gt;to combine multiple statements, use DO/END&lt;/LI&gt;
&lt;LI&gt;you had 238 observations that supplied (a) missing value(s) to your calculation. Look at line 52 of your log, and search for missing values for the variables used there.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:44:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715652#M221068</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-31T19:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715653#M221069</link>
      <description>&lt;P&gt;I'm not at a SAS station right now. However, this block of code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	if (1+s27/s2)&amp;gt;0 and (1+s27/s2) ne .  then do;
	   cf_ope_act= log(1 + s27/s2);
       lagcf_ope_act=lag(cf_ope_act);
       if first.type then lagcf_ope_act=.;
	end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;worries me. Be &lt;EM&gt;very&amp;nbsp;&lt;/EM&gt;careful when you use the Lag Function conditionally. As I've mentioned earlier, the Lag Function is really a queue that you push values through each time it is called. Therefore, it is not really a lookback function as many programmers think.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, when you use the Lag Function conditionally, you push values through the queue only when the condition is fulfilled. This usually leads to unexpected and incorrect results.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Read the article &lt;A href="https://sasnrd.com/sas-lag-function-by-group-example/" target="_self"&gt;Investigating the Lag Function&lt;/A&gt; to understand why &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:47:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715653#M221069</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-31T19:47:00Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715654#M221070</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; !&lt;BR /&gt;Thank you for your suggestion.&lt;BR /&gt;So, you mean I can replace the code of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then call missing(lags3, lags18, lags29);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then do;
 lags3 = . ;
 lags18 = . ;
 lags29= . ;
end;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is it what you mean?&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715654#M221070</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-31T19:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715656#M221072</link>
      <description>&lt;P&gt;&amp;nbsp;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The log is as below, can you please have a look, I cannot find any special variable on (line 52 and column 7) and (line 52 and column 11) from the log ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;37           *Set up lag variables;
38           data argentinalag;
39             set argentina;
40             by type;
41         	lags7=lag(s7);
42             if first.type then lags7=.;
43         	/*https://communities.sas.com/t5/SAS-Programming/Condition-of-calculating-Lag-in-a-datastep/m-p/715520#M221015*/
2                                                          The SAS System                             20:10 Friday, January 29, 2021

44             /*create other lag variables here*/
45             lags3=lag(s3);
46             if first.type then lags3=.;
47         
48         	lags18=lag(s18);
49             if first.type then lags18=.;
50         
51             *log(0) in meaningless so set these obs equalling to missing;
52         	if (1+s27/s2)&amp;gt;0 and (1+s27/s2) ne .  then do;
53         	cf_ope_act= log(1 + s27/s2);
54             lagcf_ope_act=lag(cf_ope_act);
55             if first.type then lagcf_ope_act=.;
56         	end;
57         	
58         	/*https://communities.sas.com/t5/SAS-Programming/compute-natural-logarithm-i-e-LN-in-sas/m-p/694806#M211930*/
59         
60         	lags29 = lag(s29);
61             if first.type then lags29=.;
62         
63         	lags22 = lag(s22);
64         	if first.type then lags22=.;
65         
66         	lags43 = lag(s43);
67             if first.type then lags43=.;
68            run;

NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      238 at 52:7    238 at 52:11   
NOTE: There were 1300 observations read from the data set WORK.ARGENTINA.
NOTE: The data set WORK.ARGENTINALAG has 1300 observations and 78 variables.
NOTE: Compressing data set WORK.ARGENTINALAG decreased size by 26.67 percent. 
      Compressed is 11 pages; un-compressed would require 15 pages.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.03 seconds&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:51:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715656#M221072</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-31T19:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715657#M221073</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; !&lt;BR /&gt;Thank you for your suggestion.&lt;BR /&gt;So, you mean I can replace the code of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then call missing(lags3, lags18, lags29);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then do;
 lags3 = . ;
 lags18 = . ;
 lags29= . ;
end;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is it what you mean?&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Both code snippets are equivalent, it's a matter of style preferences.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:52:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715657#M221073</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-31T19:52:46Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715659#M221074</link>
      <description>&lt;P&gt;In log line 52, you use variables s27 and s2. Inspect your dataset for missing values of those variables.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:56:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715659#M221074</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-31T19:56:30Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715660#M221075</link>
      <description>&lt;P&gt;s27 has missing values &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 19:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715660#M221075</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-31T19:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715661#M221076</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; !&lt;BR /&gt;Thank you!&lt;BR /&gt; Can I ask what do column 7 and 11 mean?&lt;BR /&gt;Many thanks!</description>
      <pubDate>Sun, 31 Jan 2021 19:57:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715661#M221076</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-31T19:57:09Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715662#M221077</link>
      <description>&lt;P&gt;You can simply that code quite a lot. Also your test for missing values is triggering the notes about operations on missing values. Use either the MISSING() or N() function to test for missing values. Test for missing values before testing for a positive result for the calculation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data argentinalag;
  set argentina;
  by type;
  lags3  = lag(s3);
  lags7  = lag(s7);
  lags18 = lag(s18);
  lags29 = lag(s29);
  lags22 = lag(s22);
  lags43 = lag(s43);
  lagcf_ope_act=lag(cf_ope_act);
  if first.type then call missing(of lags:);
  if n(s27,s2)=2 then if (1+s27/s2)&amp;gt;0 then do;
     cf_ope_act= log(1 + s27/s2);
     lagcf_ope_act=lag(cf_ope_act);
     if first.type then call missing(lagcf_ope_act);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that calling the LAG() function inside the IF/THEN DO/END block means you are lagging the values of&amp;nbsp;cf_ope_act from the previous time it was calculated and NOT form the previous observation in the source dataset.&amp;nbsp; If that is NOT what you want then change the logic to move these two statement after the conditional calculation of&amp;nbsp;cf_ope_act.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if n(s27,s2)=2 then if (1+s27/s2)&amp;gt;0 then do;
     cf_ope_act= log(1 + s27/s2);
  end;
  lagcf_ope_act=lag(cf_ope_act);
  if first.type then call missing(lagcf_ope_act);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the impact on making that change:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;Variables with Unequal Values                                                                              
                                                                                                           
Variable       Type  Len  Ndif   MaxDif  MissDif                                                           
                                                                                                           
lagcf_ope_act  NUM     8    87        0       87                                                           &lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 31 Jan 2021 20:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715662#M221077</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-31T20:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715663#M221078</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; !&lt;BR /&gt;Thank you!&lt;BR /&gt;Can I ask what do column 7 and 11 mean?&lt;BR /&gt;Many thanks!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These are also log positions, text columns within the line. So s27 and s2 seem to be missing in the same observation.&lt;/P&gt;
&lt;P&gt;I would also check that s2 is not zero before doing the division.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 20:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715663#M221078</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-31T20:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715665#M221080</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your insightful suggestion. I understand call missing to assign the missing value for specified variables. However, I do not understand the part "of lags" ?&lt;/P&gt;
&lt;P&gt;I just think the&amp;nbsp; code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.type then call missing(lagcf_ope_act);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is enough rather than&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if first.type then call missing(of lags: lagcf_ope_act);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please correct me if I explain you wrongly!&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 20:19:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715665#M221080</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-01-31T20:19:08Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715666#M221081</link>
      <description>&lt;P&gt;Read about using &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lrcon&amp;amp;docsetTarget=p0wphcpsfgx6o7n1sjtqzizp1n39.htm&amp;amp;locale=en" target="_self"&gt;variable lists&lt;/A&gt; in your code.&amp;nbsp; The string &lt;FONT face="courier new,courier"&gt;lags:&lt;/FONT&gt; means all the variables whose name starts with those four characters that have been defined as of that point in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In addition to allowing the use of a variable list like lags: the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lrcon&amp;amp;docsetTarget=n1t04bwpt1jkadn1a5mk35we2yif.htm&amp;amp;locale=en" target="_self"&gt;OF keyword in a function call&lt;/A&gt; allows you to list variables delimited by space (the same way you would in any regular SAS statement) instead of forcing you to insert commas between the variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 20:44:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715666#M221081</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-31T20:44:12Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715676#M221085</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your document suggestion and explanation and warning, it raises my awareness well taken. I one more time read it and there are some points in your document that I do not fully understand&lt;/P&gt;
&lt;P&gt;&lt;A href="https://sasnrd.com/sas-lag-function-by-group-example/" target="_blank" rel="noopener"&gt;https://sasnrd.com/sas-lag-function-by-group-example/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. So at the first page, you wrote&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Both elements are missing at the start of execution. Each time it executes, SAS returns the right-most value from the queue. Furthermore, the present value of the value we want to lag, is inserted into the queue from the left&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I&amp;nbsp; cannot visualize in my mind what is the right-most value mentioned there, and I tried to read the example afterwards but still can not get the idea thoroughly. Or you mean the right-most value is the value of result of lag2(x) ? and this result is inserted from the original data x ?&lt;/P&gt;
&lt;P&gt;2. Regarding your conditional lagging, as for your example, I also fell into the fallacy about the expectation as you described, even you give the result out , I still do not understand how come we have such returned value&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
 
id  x   Queue content
 
1   1   [ . | . ]
1   2   [ 2 | . ] Returned value: .
1   3   [ 2 | . ]
2   4   [ 4 | 2 ] Returned value: .
2   5   [ 4 | 2 ]
2   6   [ 6 | 4 ] Returned value: 2
 
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3. And I also ran your code to see the result&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   if mod(_N_, 2) = 0 then y = lag2(x);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the result is&lt;/P&gt;
&lt;DIV id="tinyMceEditorMy97_0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="tinyMceEditorMy97_1" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;Does it equals to the code below ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if mod(_N_, 2) = 0 then y = lag2(x);
else lag2(x) =.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And thank you for introducing me the function ifn, it is so powerful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Regarding your code in "Handling By-Groups" part, you have a code as below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by id;
   lagx = ifn(first.id, ., lag(x));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is really aesthetic, but I just want to cross-check if it equals to the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by id;
    lagx = lag(x);
    if first.id then lagx=.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 01:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715676#M221085</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-01T01:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715678#M221086</link>
      <description>&lt;P&gt;Best way to learn is to try it and see what happens.&amp;nbsp; For example here is a simple example using SASHELP.CLASS dataset that every SAS session should have access to. This will let you compare normal IF/THEN code with IFN() function code.&amp;nbsp; I also included and example of what happens when you conditionally execute the LAG() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sashelp.class out=class ;
  by sex name ;
run;

data test;
  set class ;
  by sex;

  lag_age1 = lag(age) ;
  if first.sex then lag_age1 = .;

  lag_age2=ifn(first.sex,.,lag(age));

  if first.sex then lag_age_wrong = .;
  else lag_age_wrong=lag(age);

run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                                                            lag_age_
Obs    Name       Sex    Age    Height    Weight    lag_age1    lag_age2      wrong

  1    Alice       F      13     56.5       84.0        .           .           .
  2    Barbara     F      13     65.3       98.0       13          13           .
  3    Carol       F      14     62.8      102.5       13          13          13
  4    Jane        F      12     59.8       84.5       14          14          14
  5    Janet       F      15     62.5      112.5       12          12          12
  6    Joyce       F      11     51.3       50.5       15          15          15
  7    Judy        F      14     64.3       90.0       11          11          11
  8    Louise      F      12     56.3       77.0       14          14          14
  9    Mary        F      15     66.5      112.0       12          12          12
 10    Alfred      M      14     69.0      112.5        .           .           .
 11    Henry       M      14     63.5      102.5       14          14          15
 12    James       M      12     57.3       83.0       14          14          14
 13    Jeffrey     M      13     62.5       84.0       12          12          12
 14    John        M      12     59.0       99.5       13          13          13
 15    Philip      M      16     72.0      150.0       12          12          12
 16    Robert      M      12     64.8      128.0       16          16          16
 17    Ronald      M      15     67.0      133.0       12          12          12
 18    Thomas      M      11     57.5       85.0       15          15          15
 19    William     M      15     66.5      112.0       11          11          11&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Jan 2021 23:17:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715678#M221086</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-31T23:17:33Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715707#M221093</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks for your dedicated explanation. Two ways of doing safely with lag are: using &lt;STRONG&gt;ifn&lt;/STRONG&gt;&amp;nbsp;or &lt;STRONG&gt;using lag with the condition at the end&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;But I still cannot explain myself why we put the condition prior to the lag generation code, the result getting wrong as in your code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.sex then lag_age_wrong = .;
  else lag_age_wrong=lag(age);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am wondering if you can help me to explain it out.&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Feb 2021 05:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715707#M221093</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-02-01T05:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: Missing values were generated as a result of performing an operation on missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715717#M221096</link>
      <description>&lt;P&gt;Make yourself some manipulatives so you can see what is happening.&amp;nbsp; For example take a deck of cards. For simplicity just use the 2 through 5.&amp;nbsp; Order the deck by suit and order within suit.&amp;nbsp; Place the cards face up.&amp;nbsp; This is your input dataset.&lt;/P&gt;
&lt;P&gt;Take a piece of paper and mark three other places to put cards.&amp;nbsp; Label these CARD,&amp;nbsp; LAG(CARD) and LAG_CARD.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now run this data step.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by suit;
  lag_card=lag(card);
  if first.suit then lag_card=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;For the SET option you will want to:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;remove any card in the LAG_CARD location (LAG_CARD is not retained).&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;take the top card from the deck and move it to CARD location.&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;Record the value of CARD for this iteration now since we are going to physically move the card but the code never modifies age.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;SPAN style="font-family: inherit;"&gt;For the assignment statement.&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face="inherit"&gt;Take the TOP card from pile in the LAG(CARD) location and move it to the LAG_CARD location (this is the result of the &lt;/FONT&gt;assignment statement&lt;FONT face="inherit"&gt;).&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;Move the card in the CARD location to the BOTTOM of the pile in the LAG(CARD) location.&amp;nbsp; This is pushing the vlaue of CARD into the stack/queue.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;If it is the first card of the suit then&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;remove the card from the LAG_CARD location.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;This ends the iteration so the current value of LAG_CARD is written&lt;/STRONG&gt; to the dataset (as it the value of the CARD you pulled for this iteration.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Repeat for all of the cards in the deck.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now let's do the experiment with the re-ordered statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by suit;
  if first.suit then lag_card=;
  else lag_card=lag(card);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;For the SET option you will want to:&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;remove any card in the LAG_CARD location (LAG_CARD is not retained).&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;take the top card from the deck and move it to CARD location.&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;Record the value of CARD for this iteration now since we are going to physically move the card but the code never modifies age.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Now let's see what happens in the IF/THEN/ELSE.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;IF it is the first card in the suit THEN&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;remove the CARD from the LAG_CARD location. (Notice that this never actually does anything since you already removed that card as part of the SET statement).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;I&lt;STRONG&gt;F it is NOT the first card in the suit then&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face="inherit"&gt;Take the TOP card from pile in the LAG(CARD) location and move it to the LAG_CARD location (this is the result of the &lt;/FONT&gt;assignment statement&lt;FONT face="inherit"&gt;).&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN style="font-family: inherit;"&gt;Move the card in the CARD location to the BOTTOM of the pile in the LAG(CARD) location.&amp;nbsp; This is pushing the vlaue of CARD into the stack/queue.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;STRONG&gt;This ends the iteration so the current value of LAG_CARD is written&lt;/STRONG&gt; to the dataset (as it the value of the CARD you pulled for this iteration.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Repeat for all of the cards in the deck.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Notice that in both the result for LAG_CARD in the first of each suit is the same.&amp;nbsp; But when you skip adding a card to pile for the first card in a suit then that card is not there to pull back out when you get to the second card.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Try with some suits only having one card.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Here is a way to simulate suit/card game with SAS code.&amp;nbsp; The four numbers in the first DO statement are the number of cards in the four suits. Try it with some of them having only 1 card.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do n=4,4,4,4;
    suit+1;
    do card=1 to n;
      output;
    end;
  end;
run;

data want;
 set have;
 by suit;
 lag_card=lag(card);
 if first.suit then lag_card=.;
run;

proc print;
run;

data want;
 set have;
 by suit;
 if not first.suit then lag_card=lag(card);
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Feb 2021 06:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Missing-values-were-generated-as-a-result-of-performing-an/m-p/715717#M221096</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-01T06:40:19Z</dc:date>
    </item>
  </channel>
</rss>

