<?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: calculate number of distinct values between rows for each customer in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601890#M174173</link>
    <description>&lt;P&gt;And if the objective of the question is to find alternatives to PROC SQL, here is a data step approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl2;
    array _ {999999} _temporary_;
    do until (last.id);
        set tbl1;
        by id;
        _[x]=1;
    end;
    do until (last.id);
        set tbl1;
        by id;
        dis_x=n(of _[*]);
        output;
    end;
    call missing(of _[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Nov 2019 07:34:06 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-11-06T07:34:06Z</dc:date>
    <item>
      <title>calculate number of distinct values between rows for each customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601887#M174170</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I have a data set where for each customer there are multiple rows.&lt;/P&gt;
&lt;P&gt;In data set there are 2 columns: ID,X .&lt;/P&gt;
&lt;P&gt;Target: Create a new data set that will calculate for each customer number of distinct values&amp;nbsp; of X&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;For customer 1 there are 2 distinct values (10,20)&lt;/P&gt;
&lt;P&gt;For customer 2 there are 3 distinct values (10,15,30)&lt;/P&gt;
&lt;P&gt;For customer 2 there is&amp;nbsp; 1 distinct values (40)&lt;/P&gt;
&lt;P&gt;I want that the new variable values will appear in all rows for each customer(I think it is called remerge in sas language).&lt;/P&gt;
&lt;P&gt;Please find a solution .&lt;/P&gt;
&lt;P&gt;My question is if there is another way with one step to do it&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data tbl1;
input ID x;
cards;
1 10
1 10
1 10
1 20
1 20
1 20
2 10
2 15
2 15
2 15
2 30
2 30
3 40
3 40
3 40
3 40
3 40
3 40
;
run;

PROC SQL;
	create table tbl2  as
	select ID, count(distinct X) as dis_x 	   
	from  tbl1
	group by ID
;
QUIT;

PROC SQL;
	create table tbl3  as
	select a.*,b.dis_x 	   
	from  tbl1 as a
	left join tbl2 as b
	on a.ID=b.ID
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Nov 2019 07:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601887#M174170</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-11-06T07:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of distinct values between rows for each customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601888#M174171</link>
      <description>&lt;P&gt;You don't need to change your code much&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
    create table tbl2 as
    select *, count(distinct x) as dis_x
    from tbl1
    group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID X   dis_X
1  10  2
1  20  2
1  10  2
1  20  2
1  20  2
1  10  2
2  10  3
2  30  3
2  30  3
2  15  3
2  15  3
2  15  3
3  40  1
3  40  1
3  40  1
3  40  1
3  40  1
3  40  1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 07:27:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601888#M174171</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-06T07:27:52Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of distinct values between rows for each customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601890#M174173</link>
      <description>&lt;P&gt;And if the objective of the question is to find alternatives to PROC SQL, here is a data step approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tbl2;
    array _ {999999} _temporary_;
    do until (last.id);
        set tbl1;
        by id;
        _[x]=1;
    end;
    do until (last.id);
        set tbl1;
        by id;
        dis_x=n(of _[*]);
        output;
    end;
    call missing(of _[*]);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Nov 2019 07:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601890#M174173</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-06T07:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of distinct values between rows for each customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601892#M174175</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Key-indexing rules ... when applicable, of course.&lt;/P&gt;
&lt;P&gt;Failing the latter, can always do a hash table instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 07:46:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601892#M174175</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-06T07:46:29Z</dc:date>
    </item>
    <item>
      <title>Re: calculate number of distinct values between rows for each customer</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601893#M174176</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;I agree. Just wanted to point out that there are many alternatived to the SQL.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 07:52:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-number-of-distinct-values-between-rows-for-each/m-p/601893#M174176</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-06T07:52:58Z</dc:date>
    </item>
  </channel>
</rss>

