<?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: assign the same value by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717934#M222101</link>
    <description>&lt;P&gt;Here's an approach.&amp;nbsp; You might have to adjust for the actual variables you have in real life, and which ones should be replicated across observations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   do until (last.id);
      update have(obs=0) have;
      by ID;
   end;
   do until (last.id);
      set have (keep=id);&lt;BR /&gt;      by id;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The top loop accumulates the variable values, then the bottom loop outputs the appropriate number of times.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Feb 2021 15:39:52 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2021-02-09T15:39:52Z</dc:date>
    <item>
      <title>assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717919#M222095</link>
      <description>&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID 00&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID 00&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD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D&gt;ID 00&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID 01&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ID 01&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Y&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you for your help&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 14:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717919#M222095</guid>
      <dc:creator>Jedrzej</dc:creator>
      <dc:date>2021-02-09T14:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717923#M222096</link>
      <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
  by ID;  /* assumed data is sorted by ID */
      retain v1 v2 v3;  /* or v1-v3 */
      if first.ID then do;
         v1=var1; v2=var2; v3=var3; /* can be done by an array and a loop */
      end;
      var1=v1; var2=v2; var3=v3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Feb 2021 14:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717923#M222096</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-02-09T14:58:28Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717925#M222097</link>
      <description>&lt;P&gt;There you go&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm=',' dsd;
input ID $ var1 $ var2 $ var3 $;
datalines;
ID 00, ,Y,  
ID 00, , ,  
ID 00,Y, ,  
ID 01,Y, ,Y 
ID 01, , ,  
;

data want;
   update have(obs=0) have;
   by ID;
   output;
run;&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     var1  var2  var3 
ID 00        Y       
ID 00        Y       
ID 00  Y     Y       
ID 01  Y           Y 
ID 01  Y           Y &lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Feb 2021 15:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717925#M222097</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-09T15:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717929#M222099</link>
      <description>I don't understand how that update statement knows that VAR1 should be set to Y on the first row ouput.&lt;BR /&gt;Same problem for row4 and VAR3</description>
      <pubDate>Tue, 09 Feb 2021 15:07:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717929#M222099</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2021-02-09T15:07:15Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717930#M222100</link>
      <description>Row4 is not at same problem as Row1. Sorry</description>
      <pubDate>Tue, 09 Feb 2021 15:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717930#M222100</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2021-02-09T15:09:21Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717934#M222101</link>
      <description>&lt;P&gt;Here's an approach.&amp;nbsp; You might have to adjust for the actual variables you have in real life, and which ones should be replicated across observations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   do until (last.id);
      update have(obs=0) have;
      by ID;
   end;
   do until (last.id);
      set have (keep=id);&lt;BR /&gt;      by id;
      output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The top loop accumulates the variable values, then the bottom loop outputs the appropriate number of times.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 15:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717934#M222101</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-02-09T15:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717948#M222103</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/107827"&gt;@Jedrzej&lt;/a&gt;&amp;nbsp;My bad, I misread the question.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;s correction is the way to go.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 17:00:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/717948#M222103</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-09T17:00:55Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/718214#M222229</link>
      <description>&lt;P&gt;If data like this , you code wouldn't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
infile datalines dlm=',' dsd;
input ID $ var1 $ var2 $ var3 $;
datalines;
ID 00, , , 
ID 00, ,Y,   
ID 00,Y, ,  
ID 01,Y, ,Y 
ID 01, , ,  
;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Feb 2021 12:40:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/718214#M222229</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-02-10T12:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/718215#M222230</link>
      <description>&lt;P&gt;As stated below, I am aware &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;s correction does the trick&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 12:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/718215#M222230</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-10T12:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/870510#M343814</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if instead of one variable to group by I have two or more?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thnk you.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2023 09:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/870510#M343814</guid>
      <dc:creator>ramgouveia</dc:creator>
      <dc:date>2023-04-19T09:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: assign the same value by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/870837#M343990</link>
      <description>&lt;P&gt;It depends on what you mean when you say you have two or more variables.&amp;nbsp; So give an example.&amp;nbsp; Certainly the BY statement supports multiple variables, but the outcome might not be what you have in mind.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2023 16:14:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assign-the-same-value-by-group/m-p/870837#M343990</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-04-20T16:14:04Z</dc:date>
    </item>
  </channel>
</rss>

