<?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: New variable based on all rows within a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767574#M243364</link>
    <description>&lt;P&gt;Here's the more convoluted way by storing min and max variables of each by group in new columns called min_loc and max_loc. When min_loc=max_loc, then you can set location2=location. When they're not equal, you can set it to "Both".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input person $4. location $20.;
datalines;
1001 DC	
1007 Maryland	
1007 DC	
2364 Maryland	
2364 Maryland	
3331 Maryland	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
1034 Maryland	
1034 DC	
1035 Maryland	
1035 DC
;
run;

data min;
	set have;
	by person;
	length min_loc  $20;
	retain min_loc ;
	if first.person then do;
		min_loc='';
	end;
	if first.person then min_loc=location;
run;

proc sort data=min;
	by person descending location;
run;

data max;
	set min;
	by person;
	length max_loc location2 $20;
	retain max_loc;
	if first.person then do;
		max_loc='';
	end;
	if first.person then max_loc=location;
	
	if min_loc ^= max_loc then location2="Both";
	if min_loc=max_loc then location2=location;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 13 Sep 2021 22:23:34 GMT</pubDate>
    <dc:creator>tarheel13</dc:creator>
    <dc:date>2021-09-13T22:23:34Z</dc:date>
    <item>
      <title>New variable based on all rows within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767546#M243354</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below dataset and trying to create a new variable&amp;nbsp; (Location2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;looking at every row for each person,&lt;BR /&gt;if there is only one row of data per person, then location2 = location&lt;BR /&gt;if there are multiple rows of data per person and location is consistent for each person, then location2 = location&lt;BR /&gt;if there are multiple rows of data per person but location is not consistent, then location2 = 'Both'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried using a do-loop but it isn't quite working as I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Person&lt;/TD&gt;&lt;TD&gt;date&lt;/TD&gt;&lt;TD&gt;Location&lt;/TD&gt;&lt;TD&gt;Location2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;TD&gt;20190629&lt;/TD&gt;&lt;TD&gt;DC&lt;/TD&gt;&lt;TD&gt;DC&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1007&lt;/TD&gt;&lt;TD&gt;20190124&lt;/TD&gt;&lt;TD&gt;Maryland&lt;/TD&gt;&lt;TD&gt;Both&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1007&lt;/TD&gt;&lt;TD&gt;20191105&lt;/TD&gt;&lt;TD&gt;DC&lt;/TD&gt;&lt;TD&gt;Both&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2364&lt;/TD&gt;&lt;TD&gt;20191203&lt;/TD&gt;&lt;TD&gt;Maryland&lt;/TD&gt;&lt;TD&gt;MD&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2364&lt;/TD&gt;&lt;TD&gt;20190405&lt;/TD&gt;&lt;TD&gt;Maryland&lt;/TD&gt;&lt;TD&gt;MD&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3331&lt;/TD&g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;/TD&gt;&lt;TD&gt;20191019&lt;/TD&gt;&lt;TD&gt;DC&lt;/TD&gt;&lt;TD&gt;Both&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1035&lt;/TD&gt;&lt;TD&gt;20190301&lt;/TD&gt;&lt;TD&gt;Maryland&lt;/TD&gt;&lt;TD&gt;Both&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1035&lt;/TD&gt;&lt;TD&gt;20191201&lt;/TD&gt;&lt;TD&gt;DC&lt;/TD&gt;&lt;TD&gt;Both&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 13 Sep 2021 20:07:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767546#M243354</guid>
      <dc:creator>Etoo12121</dc:creator>
      <dc:date>2021-09-13T20:07:44Z</dc:date>
    </item>
    <item>
      <title>Re: New variable based on all rows within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767549#M243356</link>
      <description>&lt;P&gt;Can you please post your data in a data step?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Sep 2021 20:27:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767549#M243356</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-13T20:27:50Z</dc:date>
    </item>
    <item>
      <title>Re: New variable based on all rows within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767557#M243358</link>
      <description>&lt;P&gt;You did not provide any rule for converting Maryland into MD.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
    select *
          , case when (count(distinct location)&amp;lt;2) then location else 'Both' end as location2
    from have
    group by person
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    Person          date    Location    expected    location2

  1     1001     2019-06-29    DC            DC        DC
  2     1007     2019-11-05    DC            Both      Both
  3     1007     2019-01-24    Maryland      Both      Both
  4     1034     2019-02-19    Maryland      Both      Both
  5     1034     2019-10-19    DC            Both      Both
  6     1035     2019-12-01    DC            Both      Both
  7     1035     2019-03-01    Maryland      Both      Both
  8     2364     2019-04-05    Maryland      MD        Maryland
  9     2364     2019-12-03    Maryland      MD        Maryland
 10     3331     2019-01-01    Maryland      MD        Maryland
 11     6579     2019-03-25    DC            DC        DC
 12     6579     2019-11-01    DC            DC        DC
 13     6579     2019-08-07    DC            DC        DC
 14     6579     2019-07-17    DC            DC        DC
 15     6579     2019-05-04    DC            DC        DC
 16     6579     2019-04-16    DC            DC        DC
 17     6579     2019-04-04    DC            DC        DC
&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Sep 2021 20:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767557#M243358</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-13T20:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: New variable based on all rows within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767563#M243359</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Assign-value-based-on-status/m-p/765914" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Assign-value-based-on-status/m-p/765914&lt;/A&gt;&amp;nbsp;similar question has been asked.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Sep 2021 21:01:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767563#M243359</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-13T21:01:38Z</dc:date>
    </item>
    <item>
      <title>Re: New variable based on all rows within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767574#M243364</link>
      <description>&lt;P&gt;Here's the more convoluted way by storing min and max variables of each by group in new columns called min_loc and max_loc. When min_loc=max_loc, then you can set location2=location. When they're not equal, you can set it to "Both".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input person $4. location $20.;
datalines;
1001 DC	
1007 Maryland	
1007 DC	
2364 Maryland	
2364 Maryland	
3331 Maryland	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
6579 DC	
1034 Maryland	
1034 DC	
1035 Maryland	
1035 DC
;
run;

data min;
	set have;
	by person;
	length min_loc  $20;
	retain min_loc ;
	if first.person then do;
		min_loc='';
	end;
	if first.person then min_loc=location;
run;

proc sort data=min;
	by person descending location;
run;

data max;
	set min;
	by person;
	length max_loc location2 $20;
	retain max_loc;
	if first.person then do;
		max_loc='';
	end;
	if first.person then max_loc=location;
	
	if min_loc ^= max_loc then location2="Both";
	if min_loc=max_loc then location2=location;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Sep 2021 22:23:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767574#M243364</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-13T22:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: New variable based on all rows within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767590#M243376</link>
      <description>This worked perfectly</description>
      <pubDate>Tue, 14 Sep 2021 00:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/New-variable-based-on-all-rows-within-a-group/m-p/767590#M243376</guid>
      <dc:creator>Etoo12121</dc:creator>
      <dc:date>2021-09-14T00:54:28Z</dc:date>
    </item>
  </channel>
</rss>

