<?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 Creating new variable, depending on other variables in different rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943633#M369816</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_have;
  infile datalines dlm='|' ;
  input id :$20. year count;
datalines;

x1 | 2020 | 1 
x2 | 2017 | 1 
x3 | 2019 | 1 
x4 | 2017 | 1 
x5 | 2015 | 1 
x6 | 2013 | 1 
x6 | 2014 | 2 
x6 | 2015 | 3 
x6 | 2018 | 4 
x7 | 2017 | 1 
x8 | 2020 | 1 
x9 | 2021 | 1 
x9 | 2021 | 2 
x10 | 2022 | 1 
x10 | 2022 | 2 
x11 | 2012 | 1 
x12 | 2022 | 1 
x13 | 2022 | 1 
x14 | 2021 | 1 
x15 | 2012 | 1 
x16 | 2020 | 1 
x16 | 2022 | 2 
x17 | 2019 | 1 
x18 | 2017 | 1 
x18 | 2017 | 2 
x19 | 2016 | 1 
x20 | 2014 | 1 
x21 | 2018 | 1 
x22 | 2017 | 1 
x22 | 2021 | 2 
x23 | 2020 | 1 
x24 | 2018 | 1 
x24 | 2021 | 2 
x25 | 2016 | 1 
x26 | 2022 | 1 
x26 | 2022 | 2 
x27 | 2013 | 1 
x28 | 2016 | 1 
x28 | 2016 | 2 
x29 | 2017 | 1 
x29 | 2021 | 2 
x30 | 2018 | 1 
x31 | 2012 | 1 
x31 | 2012 | 2 
x32 | 2018 | 1 
x33 | 2014 | 1 
x34 | 2021 | 1 
x35 | 2014 | 1 
x35 | 2014 | 2 
x36 | 2015 | 1 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like a new variable N_COUNT, wich indicates the highest number of the variable COUNT within each ID. This variable (N_COUNT) should be expanded on each row of the corresponding ID, resulting as following dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id | year | count | n_count
x1 | 2020 | 1     | 1
x2 | 2017 | 1     | 1
x3 | 2019 | 1     | 1
x4 | 2017 | 1     | 1
x5 | 2015 | 1     | 1
x6 | 2013 | 1     | 4
x6 | 2014 | 2     | 4
x6 | 2015 | 3     | 4
x6 | 2018 | 4     | 4
x7 | 2017 | 1     | 1
x8 | 2020 | 1     | 1
x9 | 2021 | 1     | 2
x9 | 2021 | 2     | 2
x10 | 2022 | 1    | 2
x10 | 2022 | 2    | 2
x11 | 2012 | 1    | 1
x12 | 2022 | 1    | 1
x13 | 2022 | 1    | 1
x14 | 2021 | 1    | 1
x15 | 2012 | 1    | 1
x16 | 2020 | 1    | 2
x16 | 2022 | 2    | 2
x17 | 2019 | 1    | 1
x18 | 2017 | 1    | 2
x18 | 2017 | 2    | 2
x19 | 2016 | 1    | 1
x20 | 2014 | 1    | 1
x21 | 2018 | 1    | 1
x22 | 2017 | 1    | 2
x22 | 2021 | 2    | 2
x23 | 2020 | 1    | 1
x24 | 2018 | 1    | 2
x24 | 2021 | 2    | 2
x25 | 2016 | 1    | 1
x26 | 2022 | 1    | 2
x26 | 2022 | 2    | 2
x27 | 2013 | 1    | 1
x28 | 2016 | 1    | 2
x28 | 2016 | 2    | 2
x29 | 2017 | 1    | 2
x29 | 2021 | 2    | 2
x30 | 2018 | 1    | 1
x31 | 2012 | 1    | 2
x31 | 2012 | 2    | 2
x32 | 2018 | 1    | 1
x33 | 2014 | 1    | 1
x34 | 2021 | 1    | 1
x35 | 2014 | 1    | 2
x35 | 2014 | 2    | 2
x36 | 2015 | 1    | 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What command can I use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Sep 2024 12:59:04 GMT</pubDate>
    <dc:creator>Tamino</dc:creator>
    <dc:date>2024-09-12T12:59:04Z</dc:date>
    <item>
      <title>Creating new variable, depending on other variables in different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943633#M369816</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have following dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_have;
  infile datalines dlm='|' ;
  input id :$20. year count;
datalines;

x1 | 2020 | 1 
x2 | 2017 | 1 
x3 | 2019 | 1 
x4 | 2017 | 1 
x5 | 2015 | 1 
x6 | 2013 | 1 
x6 | 2014 | 2 
x6 | 2015 | 3 
x6 | 2018 | 4 
x7 | 2017 | 1 
x8 | 2020 | 1 
x9 | 2021 | 1 
x9 | 2021 | 2 
x10 | 2022 | 1 
x10 | 2022 | 2 
x11 | 2012 | 1 
x12 | 2022 | 1 
x13 | 2022 | 1 
x14 | 2021 | 1 
x15 | 2012 | 1 
x16 | 2020 | 1 
x16 | 2022 | 2 
x17 | 2019 | 1 
x18 | 2017 | 1 
x18 | 2017 | 2 
x19 | 2016 | 1 
x20 | 2014 | 1 
x21 | 2018 | 1 
x22 | 2017 | 1 
x22 | 2021 | 2 
x23 | 2020 | 1 
x24 | 2018 | 1 
x24 | 2021 | 2 
x25 | 2016 | 1 
x26 | 2022 | 1 
x26 | 2022 | 2 
x27 | 2013 | 1 
x28 | 2016 | 1 
x28 | 2016 | 2 
x29 | 2017 | 1 
x29 | 2021 | 2 
x30 | 2018 | 1 
x31 | 2012 | 1 
x31 | 2012 | 2 
x32 | 2018 | 1 
x33 | 2014 | 1 
x34 | 2021 | 1 
x35 | 2014 | 1 
x35 | 2014 | 2 
x36 | 2015 | 1 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would like a new variable N_COUNT, wich indicates the highest number of the variable COUNT within each ID. This variable (N_COUNT) should be expanded on each row of the corresponding ID, resulting as following dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id | year | count | n_count
x1 | 2020 | 1     | 1
x2 | 2017 | 1     | 1
x3 | 2019 | 1     | 1
x4 | 2017 | 1     | 1
x5 | 2015 | 1     | 1
x6 | 2013 | 1     | 4
x6 | 2014 | 2     | 4
x6 | 2015 | 3     | 4
x6 | 2018 | 4     | 4
x7 | 2017 | 1     | 1
x8 | 2020 | 1     | 1
x9 | 2021 | 1     | 2
x9 | 2021 | 2     | 2
x10 | 2022 | 1    | 2
x10 | 2022 | 2    | 2
x11 | 2012 | 1    | 1
x12 | 2022 | 1    | 1
x13 | 2022 | 1    | 1
x14 | 2021 | 1    | 1
x15 | 2012 | 1    | 1
x16 | 2020 | 1    | 2
x16 | 2022 | 2    | 2
x17 | 2019 | 1    | 1
x18 | 2017 | 1    | 2
x18 | 2017 | 2    | 2
x19 | 2016 | 1    | 1
x20 | 2014 | 1    | 1
x21 | 2018 | 1    | 1
x22 | 2017 | 1    | 2
x22 | 2021 | 2    | 2
x23 | 2020 | 1    | 1
x24 | 2018 | 1    | 2
x24 | 2021 | 2    | 2
x25 | 2016 | 1    | 1
x26 | 2022 | 1    | 2
x26 | 2022 | 2    | 2
x27 | 2013 | 1    | 1
x28 | 2016 | 1    | 2
x28 | 2016 | 2    | 2
x29 | 2017 | 1    | 2
x29 | 2021 | 2    | 2
x30 | 2018 | 1    | 1
x31 | 2012 | 1    | 2
x31 | 2012 | 2    | 2
x32 | 2018 | 1    | 1
x33 | 2014 | 1    | 1
x34 | 2021 | 1    | 1
x35 | 2014 | 1    | 2
x35 | 2014 | 2    | 2
x36 | 2015 | 1    | 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What command can I use?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 12:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943633#M369816</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2024-09-12T12:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable, depending on other variables in different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943635#M369817</link>
      <description>&lt;P&gt;It will help people help you if you can share the code you've tried, and describe how it went wrong.&amp;nbsp; There are different ways to approach this.&amp;nbsp; Are you looking for a DATA step approach?&amp;nbsp; Are you familiar with PROC SQL?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 13:19:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943635#M369817</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-09-12T13:19:41Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable, depending on other variables in different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943636#M369818</link>
      <description>&lt;P&gt;I have not tried any code so far. DATA step would be finde to keep the whole dataset, I would also be fine with a PROC SQL, with which I am only little familiar.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 13:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943636#M369818</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2024-09-12T13:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable, depending on other variables in different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943664#M369829</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as
 select a.id,a.year,a.count&lt;BR /&gt;       ,b.n_count
   from dataset_have as a
        left join
       (select id,  max(count) as n_count 
        from dataset_have
        group by id
       ) as b
        on a.id=b.id
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;In the future please make sure that your data step is complete. Your datalines did not end with a ; so does not run. It is also a very good idea not to have blank lines in datalines as results are sometimes not quite as expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ID values will be default sort order for character values, i.e. x1 followed by x10.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 15:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943664#M369829</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-09-12T15:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable, depending on other variables in different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943669#M369834</link>
      <description>&lt;P&gt;SQL, Group BY clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table want as
		select*, max(count) as n_count
			from dataset_have
		group by id
	order by 1, 2;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Sep 2024 16:11:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943669#M369834</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2024-09-12T16:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variable, depending on other variables in different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943671#M369835</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/414097"&gt;@Tamino&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to preserve the order of observations, you can use either of the suggested PROC SQL steps with an ORDER BY clause like this&lt;/P&gt;
&lt;PRE&gt;order by input(compress(id,'x'),32.),year,count;&lt;/PRE&gt;
&lt;P&gt;or use a DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do _n_=1 by 1 until(last.id);
  set dataset_have;
  by id notsorted;
  n_count=max(n_count,count);
end;
do _n_=1 to _n_;
  set dataset_have;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that your DATASET_HAVE is not sorted (but only grouped) by ID, which is why I used the NOTSORTED option in the BY statement above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Sep 2024 16:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variable-depending-on-other-variables-in-different/m-p/943671#M369835</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-09-12T16:33:32Z</dc:date>
    </item>
  </channel>
</rss>

