<?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: SAS First. and Last. conditional coding in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894541#M353342</link>
    <description>&lt;P&gt;Using this logic some of it works but not the&amp;nbsp;dsp_total_display_imps counter?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the code snippet and result:&lt;/P&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_prev=lag(dsp_display_cmli_id); if dsp_display_cmli_id ^= dsp_display_prev then dsp_total_display_imps+1;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; display_prev =lag(display_cmli_id); if display_cmli_id ^= display_prev then total_display_imps+1;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; sp_prev =lag(sp_cmli_id); if sp_cmli_id ^= sp_prev then total_sp_clicks+1;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kaushansky_0-1694802972263.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88071i9B2DAC71683AC615/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Kaushansky_0-1694802972263.png" alt="Kaushansky_0-1694802972263.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2023 18:36:20 GMT</pubDate>
    <dc:creator>Kaushansky</dc:creator>
    <dc:date>2023-09-15T18:36:20Z</dc:date>
    <item>
      <title>SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894521#M353338</link>
      <description>&lt;P&gt;I am trying to use the following 4 columns to create and count new variables, using First. and Last. but I see that First. and Last. are somehow the same for the sorted variables as you can see in the temp variables and so I cannot use them to differentiate a calculation. Take a look at the PROC PRINT :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kaushansky_0-1694800244826.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88057i0C5ADFC711FAA8C1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Kaushansky_0-1694800244826.png" alt="Kaushansky_0-1694800244826.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 17:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894521#M353338</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-15T17:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894522#M353339</link>
      <description>&lt;P&gt;Your problem statement explains what doesn't work the way you want it to work. We also need to know what you are trying to do. Please explain.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 17:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894522#M353339</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-15T17:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894523#M353340</link>
      <description>&lt;P&gt;For example:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to only count 1 each time each one of the IDs is a first. E.g. First_search should only count as 1 since all of the ID values are the same, but the first. does not work in this case.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 18:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894523#M353340</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-15T18:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894528#M353341</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/373742"&gt;@Kaushansky&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to only count 1 each time each one of the IDs is a first. E.g. First_search should only count as 1 since all of the ID values are the same, but the first. does not work in this case.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;FIRST. does not seem to be the right tool in this case. Perhaps you want something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    prev=lag(fp_clmi_id);
    if fp_clmi_id ^= prev then first_search=1;
    else first_search=0;
    drop prev;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2023 18:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894528#M353341</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-15T18:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894541#M353342</link>
      <description>&lt;P&gt;Using this logic some of it works but not the&amp;nbsp;dsp_total_display_imps counter?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See the code snippet and result:&lt;/P&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_prev=lag(dsp_display_cmli_id); if dsp_display_cmli_id ^= dsp_display_prev then dsp_total_display_imps+1;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; display_prev =lag(display_cmli_id); if display_cmli_id ^= display_prev then total_display_imps+1;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; sp_prev =lag(sp_cmli_id); if sp_cmli_id ^= sp_prev then total_sp_clicks+1;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kaushansky_0-1694802972263.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88071i9B2DAC71683AC615/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Kaushansky_0-1694802972263.png" alt="Kaushansky_0-1694802972263.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 18:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894541#M353342</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-15T18:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894570#M353348</link>
      <description>&lt;P&gt;Please show us the entire SAS data step, not a portion of it. Please format the data step for readability with at most one SAS command on a line, and indented properly. Please paste the SAS code into the window that appears when you click on the "little running man" icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide data as SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions and examples&lt;/A&gt;) and not as screen captures.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 20:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894570#M353348</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-15T20:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894573#M353350</link>
      <description>&lt;DIV&gt;data &amp;amp;dataframe._1; set &amp;amp;dataframe(where=(luid=&amp;amp;qa_luid));&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; by luid&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; order_id&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_cmli_id&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; display_cmli_id&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; sp_cmli_id&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_imps_event_ts&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; display_imps_event_ts&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; sp_clicks_event_ts&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; trans_event_ts&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; ;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; if first.luid then do;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps=0;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps=0;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks=0;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; retain &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; ;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; end;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; *-- Counting total impressions and clicks by luid --*;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_prev=lag(dsp_display_cmli_id); if dsp_display_cmli_id ^=dsp_display_prev then dsp_total_display_imps+1;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; display_prev =lag(display_cmli_id); if display_cmli_id ^=display_prev then total_display_imps+1;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; sp_prev =lag(sp_cmli_id); if sp_cmli_id ^=sp_prev then total_sp_clicks+1;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; keep&amp;nbsp; luid&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; display_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; sp_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; dsp_display_prev&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; display_prev &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; sp_prev&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; ;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;proc print data=&amp;amp;dataframe._1;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; var luid&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; display_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; sp_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; dsp_display_prev&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; display_prev &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; sp_prev&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; ;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; where luid=&amp;amp;qa_luid;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kaushansky_0-1694808533846.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88086i1B781E62D80B86A1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Kaushansky_0-1694808533846.png" alt="Kaushansky_0-1694808533846.png" /&gt;&lt;/span&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 15 Sep 2023 20:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894573#M353350</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-15T20:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894582#M353355</link>
      <description>&lt;P&gt;I'm not going to work with your data as a screen capture. I need the data provided as I explained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Furthermore, you still have multiple SAS commands on a single line.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 21:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894582#M353355</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-15T21:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894606#M353371</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/373742"&gt;@Kaushansky&lt;/a&gt;&amp;nbsp;Below your code in a better readable manner using menu&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1694837769532.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88091i983FF040E1C99C76/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1694837769532.png" alt="Patrick_0-1694837769532.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;To "fix" your code and provide some tested code requires sample data to work with. Please provide such sample data that works with your code (best via a SAS data step that creates the data) so we can spend the time solving your problem. Don't expect that anyone is going to create such data for you based on a screenshot.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data &amp;amp;dataframe._1;
  set &amp;amp;dataframe(where=(luid=&amp;amp;qa_luid));
  by luid 
    order_id 
    dsp_display_cmli_id
    display_cmli_id
    sp_cmli_id
    dsp_display_imps_event_ts
    display_imps_event_ts 
    sp_clicks_event_ts
    trans_event_ts 
  ;

  if first.luid then
    do;
      total_dsp_display_imps=0;
      total_display_imps=0;
      total_sp_clicks=0;
      retain
        total_dsp_display_imps
        total_display_imps
        total_sp_clicks
      ;
    end;

  *-- Counting total impressions and clicks by luid --*;
  dsp_display_prev=lag(dsp_display_cmli_id);

  if dsp_display_cmli_id ^=dsp_display_prev then
    dsp_total_display_imps+1;
  display_prev =lag(display_cmli_id);

  if display_cmli_id ^=display_prev then
    total_display_imps+1;
  sp_prev =lag(sp_cmli_id);

  if sp_cmli_id ^=sp_prev then
    total_sp_clicks+1;
  keep  luid
    dsp_display_cmli_id
    display_cmli_id
    sp_cmli_id

    dsp_display_prev
    display_prev
    sp_prev

    total_display_imps  
    total_dsp_display_imps  
    total_sp_clicks  
  ;
run;

proc print data=&amp;amp;dataframe._1;
  var luid
    dsp_display_cmli_id
    display_cmli_id
    sp_cmli_id

    dsp_display_prev
    display_prev
    sp_prev

    total_display_imps  
    total_dsp_display_imps  
    total_sp_clicks  

  ;
  where luid=&amp;amp;qa_luid;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 16 Sep 2023 04:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894606#M353371</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-09-16T04:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894628#M353380</link>
      <description>&lt;P&gt;Attached is the data used to process the code above that does not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data UTP_REDBULL_DF_1;
  set UTP_REDBULL_DF_1;
  by luid 
    order_id 
    dsp_display_cmli_id
    display_cmli_id
    sp_cmli_id
    dsp_display_imps_event_ts
    display_imps_event_ts 
    sp_clicks_event_ts
    trans_event_ts 
  ;

  if first.luid then
    do;
      total_dsp_display_imps=0;
      total_display_imps=0;
      total_sp_clicks=0;
      retain
        total_dsp_display_imps
        total_display_imps
        total_sp_clicks
      ;
    end;

  *-- Counting total impressions and clicks by luid --*;
  dsp_display_prev=lag(dsp_display_cmli_id);

  if dsp_display_cmli_id ^=dsp_display_prev then
    dsp_total_display_imps+1;
  display_prev =lag(display_cmli_id);

  if display_cmli_id ^=display_prev then
    total_display_imps+1;
  sp_prev =lag(sp_cmli_id);

  if sp_cmli_id ^=sp_prev then
    total_sp_clicks+1;
  keep  luid
    dsp_display_cmli_id
    display_cmli_id
    sp_cmli_id

    dsp_display_prev
    display_prev
    sp_prev

    total_display_imps  
    total_dsp_display_imps  
    total_sp_clicks  
  ;
run;

proc print data=&amp;amp;dataframe._1;
  var luid
    dsp_display_cmli_id
    display_cmli_id
    sp_cmli_id

    dsp_display_prev
    display_prev
    sp_prev

    total_display_imps  
    total_dsp_display_imps  
    total_sp_clicks  

  ;
  where luid=&amp;amp;qa_luid;
run;&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 16 Sep 2023 16:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894628#M353380</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-16T16:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894645#M353392</link>
      <description>&lt;P&gt;Please provide data that works with your code.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1694914913916.png" style="width: 783px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88099i817DFDF15551C16A/image-dimensions/783x94?v=v2" width="783" height="94" role="button" title="Patrick_0-1694914913916.png" alt="Patrick_0-1694914913916.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_1-1694914938887.png" style="width: 950px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88100i35F53F5DEC49A3AF/image-dimensions/950x38?v=v2" width="950" height="38" role="button" title="Patrick_1-1694914938887.png" alt="Patrick_1-1694914938887.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also be aware that many people won't download Excels. A simple alternative way to provide such data is: Use the EG import wizard and select to embed the data. Then copy the generated code with the data in a .sas file and share it as an attachment.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_2-1694915067928.png" style="width: 391px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88101iDFC3860BC5EA2FCC/image-dimensions/391x178?v=v2" width="391" height="178" role="button" title="Patrick_2-1694915067928.png" alt="Patrick_2-1694915067928.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just based on what you've already shared see if below can take you a step closer to a resolution.&lt;/P&gt;
&lt;P&gt;What data volumes are you dealing with? How many rows do you have in your source data?&lt;/P&gt;
&lt;P&gt;And... is the result you're after a table or a report?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=UTP_REDBULL_DF_1 out=UTP_REDBULL_DF_2;
  by luid 
    display_cmli_id
    dsp_display_cmli_id
    sp_cmli_id
    ;
run;
 
data UTP_REDBULL_DF_2;
  set UTP_REDBULL_DF_2;
  by luid 
    display_cmli_id
    dsp_display_cmli_id
    sp_cmli_id
    ;

  *-- Counting total impressions and clicks by luid --*;
  if first.display_cmli_id      then dist_cnt_display_cmli_id+1;
  if first.dsp_display_cmli_id  then dist_cnt_dsp_display_cmli_id+1;
  if first.sp_cmli_id           then dist_cnt_sp_cmli_id+1;

  if last.luid then 
    do;
      output;
      call missing(of dist_cnt_:);
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2023 02:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894645#M353392</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-09-17T02:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894651#M353396</link>
      <description>&lt;P&gt;I cannot tell what you want.&amp;nbsp; Your picture seems to have proper FIRST. and LAST. variables if the BY statement has LUID followed by that DSP variable.&amp;nbsp; Whenever you are on the first observation for a value of LUID you are by definition on the first value of DSP for that particular value of LUID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What is it that you want to create instead?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: If you want to have SAS calculate FIRST. and LAST. variables when the observations are not actually sorted by those same BY variables then you need to add the NOTSORTED keyword.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example if you want to count how many times a patient changed DOSE you could sort the data by ID and DATE and then process the data by ID and DOSE with the NOTSORTED keyword and then use the FIRST.DOSE flag to detect when the dose changes.&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2023 03:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894651#M353396</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-17T03:22:09Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894680#M353407</link>
      <description>&lt;P&gt;Okay, I will be more explicit.&lt;/P&gt;
&lt;P&gt;The variable&amp;nbsp;dsp_total_display_imps should count every time the PREVIOUS record is not the same? However, that does not work in this code. Please tell me what I am doing wrong. Attached is the dataset and below is the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;data UTP_REDBULL_DF_2; set UTP_REDBULL_DF_1&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; (where=(luid=&amp;amp;qa_luid)&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; keep=luid dsp_display_cmli_id display_cmli_id sp_cmli_id);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; by luid;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; if first.luid then do;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps=0;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps=0;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks=0;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; end;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; retain &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; ;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; *-- Counting total impressions and clicks by luid --*;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_prev=lag(dsp_display_cmli_id); if dsp_display_cmli_id ^=dsp_display_prev then dsp_total_display_imps+1;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; display_prev =lag(display_cmli_id); if display_cmli_id ^=display_prev then total_display_imps+1;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp; &amp;nbsp; sp_prev =lag(sp_cmli_id); if sp_cmli_id ^=sp_prev then total_sp_clicks+1;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; keep&amp;nbsp; luid&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; dsp_display_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; display_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; sp_cmli_id &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; dsp_display_prev&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; display_prev &lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; sp_prev&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_display_imps &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_dsp_display_imps &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; total_sp_clicks &amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; ;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 17 Sep 2023 13:22:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894680#M353407</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-17T13:22:27Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894720#M353430</link>
      <description>&lt;P&gt;Simplify, simplify, simplify.&amp;nbsp; &amp;nbsp; Please make some SIMPLE data that CLEARLY shows the problem you are trying to solve and the result you expect.&amp;nbsp; Do not include more observations than you need to demonstrate the issue.&amp;nbsp; Do not include more variables than you need.&amp;nbsp; Use short simple names for the variables.&amp;nbsp; Post the data as a data step so others can have your example to play with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not know what&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The variable&amp;nbsp;dsp_total_display_imps should count every time the PREVIOUS record is not the same?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;means.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you want to count every time the WHOLE RECORD changes then include EVERY VARIABLE in the BY statement and just check the FIRST. flag for the last one.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So this example data has three distinct records (when processed in this current order).&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input luid :$10. var1 :$10. var2 :$8. var3 :$8. ;
cards;
1027774874 6261781089 3g6gzrq 1744739
1027774874 6261781089 3g6gzrq 1744739
1027774874 6261781089 56vcsen 1744739
1027774874 6261781089 56vcsen 1744739
1027774874 6261781089 b9rq6w4 1744739
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;And this code can find them.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by luid var1 var2 var3 var4 notsorted;
  distinct_records + first.var4;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;                                                         distinct_
Obs       luid          var1        var2       var3       records

 1     1027774874    6261781089    3g6gzrq    1744739        1
 2     1027774874    6261781089    3g6gzrq    1744739        1
 3     1027774874    6261781089    56vcsen    1744739        2
 4     1027774874    6261781089    56vcsen    1744739        2
 5     1027774874    6261781089    b9rq6w4    1744739        3
&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Is that what you are trying to do?&amp;nbsp; Find out that this value of LUID has 3 distinct records?&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If so then perhaps you want this data step?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by luid var1 var2 var3  notsorted;
  distinct_records + first.var3;
  if last.luid;
  keep luid distinct_records;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Results&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;                     distinct_
Obs       luid        records

 1     1027774874        3
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Sep 2023 23:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894720#M353430</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-17T23:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894727#M353434</link>
      <description>Per my earlier example. My question is why doesn’t the LAG function work properly. As you can see in the data step and the output. The dsp_display_prev=lag(dsp_display_cmli_id); if dsp_display_cmli_id ^=dsp_display_prev then dsp_total_display_imps+1;&lt;BR /&gt;&lt;BR /&gt;dsp_total_display_imps   Should be adding +1 when the previous record is different BUT it is not and all the values are zero? The solution you provided below does not solve it. My ask is as follows:&lt;BR /&gt;&lt;BR /&gt;I would like each of the new variables (e.g.  dsp_total_display_imps ) to count the unique records for each of the CLMI variables in the file. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 18 Sep 2023 02:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894727#M353434</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-18T02:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894782#M353461</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I would like each of the new variables (e.g. dsp_total_display_imps ) to count the unique records for each of the CLMI variables in the file.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So that is a different question and cannot be handled by either BY group processing nor by just checking if the current value is different than the previous value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do this with SQL by using the DISTINCT keyword and the COUNT() function (although that will not count the missing values).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select count(distinct var1) as n_var1
     , count(distinct var2) as n_var2
     , count(distinct_var3) as n_var3
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you want to do it with a data step will require a hash object (or perhaps a series of hash objects).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the variables are all of the same type you might be able to use arrays and a single hash object to make the coding easier/shorter.&lt;/P&gt;
&lt;P&gt;For example if the variables that you want to count the distinct values are all character with lengths less than or equal to 20 the code might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have end=eof;
  array orig [3] $20 var1 var2 var3;
  array new [3] n_var1 n_var2 n_var3;
  retain n_var1 0 n_var2 0 n_var3 0;
  length value $20;
  if _n_=1 then do;
     declare hash h();
     h.definekey('index','value');
     h.definedata('index');
     h.definedone();
  end;
  do index=1 to dim(orig);
    value=orig[index];
    if h.add() then new[index]+1;
  end;
  keep n_var1 n_var2 n_var3;
  if eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So using the example data from before:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input luid :$10. var1 :$10. var2 :$8. var3 :$8. ;
cards;
1027774874 6261781089 3g6gzrq 1744739
1027774874 6261781089 3g6gzrq 1744739
1027774874 6261781089 56vcsen 1744739
1027774874 6261781089 56vcsen 1744739
1027774874 6261781089 b9rq6w4 1744739
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs    n_var1    n_var2    n_var3

 1        4         2         4

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 13:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894782#M353461</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-18T13:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894794#M353467</link>
      <description>&lt;P&gt;Thank you that works. Now what is I only wanted to count if a value is unique ONCE. Meaning, instead of how many unique, I only want to count 1 across all unique value.&amp;nbsp; Would prev(var1)=var1 work?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Sep 2023 15:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894794#M353467</guid>
      <dc:creator>Kaushansky</dc:creator>
      <dc:date>2023-09-18T15:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS First. and Last. conditional coding</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894816#M353473</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/373742"&gt;@Kaushansky&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you that works. Now what is I only wanted to count if a value is unique ONCE. Meaning, instead of how many unique, I only want to count 1 across all unique value.&amp;nbsp; Would prev(var1)=var1 work?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you no longer want to COUNT?&amp;nbsp; Instead you just want to FLAG the first occurrence of a value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just change how to assign values to the new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's make some test data with more distinct values of the variables and try it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input luid :$10. var1 :$10. var2 :$8. var3 :$8. ;
cards;
1027774874 6261781089 3g6gzrq 1744739
1027774874 6261781089 3g6gzrq 8888888
1027774874 6261781089 56vcsen 9999999
1027774874 9999999999 56vcsen 1744739
1027774874 6261781089 b9rq6w4 1744739
;

data want;
  set have ;
  array orig [3] $20 var1 var2 var3;
  array new [3] n_var1 n_var2 n_var3;
  retain n_var1 0 n_var2 0 n_var3 0;
  length value $20;
  if _n_=1 then do;
     declare hash h();
     h.definekey('index','value');
     h.definedata('index');
     h.definedone();
  end;
  do index=1 to dim(orig);
    value=orig[index];
    if h.add() then new[index]=0;
    else new[index]=1;
  end;
  drop index value;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;Obs       luid          var1        var2       var3      n_var1    n_var2    n_var3

 1     1027774874    6261781089    3g6gzrq    1744739       1         1         1
 2     1027774874    6261781089    3g6gzrq    8888888       0         0         1
 3     1027774874    6261781089    56vcsen    9999999       0         1         1
 4     1027774874    9999999999    56vcsen    1744739       1         0         0
 5     1027774874    6261781089    b9rq6w4    1744739       0         1         0

&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Sep 2023 16:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-First-and-Last-conditional-coding/m-p/894816#M353473</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-18T16:16:56Z</dc:date>
    </item>
  </channel>
</rss>

