<?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: create a new variable based on 2 other variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571452#M161196</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;is right that this is a typical job for the DoW loop, however the latter requires explicit sorting by VAR2. If you don't mind sorting, then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                           
  input id var1 var2 ;                
  cards ;                             
1  23  1                              
2  12  1                              
3  14  2                              
4  33  1                              
5  18  3                              
6  20  2                              
7  45  3                              
run ;                                 
                                      
proc sort data = have out = havesort ;
  by var2 id ;                        
run ;                                 
                                      
data want_dow (drop = _:) ;           
  do _n_ = 1 by 1 until (last.var2) ; 
    set havesort (keep = var:) ;      
    by var2 ;                         
    _s = sum (_s, var1) ;             
  end ;                               
  newvar = divide (_s, _n_) ;         
  do _n_ = 1 to _n_ ;                 
    set havesort ;                    
    output ;                          
  end ;                               
run ;                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to preserve the original data order without sorting forth and back, you can first aggregate the sums and counts using the hash object, then look it up for each record from&amp;nbsp; HAVE and compute the means:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_hash (drop = _:) ;                       
  if _n_ = 1 then do ;                             
    dcl hash h () ;                                
    h.definekey ("var2") ;                         
    h.definedata ("_s", "_q") ;                    
    h.definedone () ;                              
    do until (z) ;                                 
      set have (keep = var:) end = z ;             
      if h.find() ne 0 then call missing (_s, _q) ;
      _s = sum (_s, var1) ;                        
      _q = sum (_q, 1) ;                           
      h.replace() ;                                
    end ;                                          
  end ;                                            
  set have ;                                       
  h.find() ;                                       
  newvar = divide (_s, _q) ;                       
run ;                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, perhaps the simplest way (also suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;) is just to use the self-merging property of SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                                   
  create table want_sql as                   
  select id, var2, var1, avg (var1) as newvar
  from   have                                
  group  var2                                
  order  var2, id                            
  ;                                          
quit ;                                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Jul 2019 17:09:08 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-07-05T17:09:08Z</dc:date>
    <item>
      <title>create a new variable based on 2 other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571390#M161172</link>
      <description>&lt;P&gt;hi everyone,&lt;/P&gt;&lt;P&gt;here is my data&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp;var1 var2&amp;nbsp; &amp;nbsp; newvar&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;23&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;12&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp;&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;14&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp;33&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; 18&amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; 45&amp;nbsp; &amp;nbsp; 3&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;Var1 is a continuous variable, and Var2 is a categorical variable (3 categories). For 1st category (var2=1), the newvar= mean of var 1 among the people in the 1st category. For the 2nd category (var2=2), the newvar=mean of var2 among the people in the 2nd category. For the 3rd category, the I just want to newvar=var1; Therefore the newvar will be assigned with the same value for 1st category, and with another same value for 2nd category.&lt;/P&gt;&lt;P&gt;I know I can do this by running proc means to calculate the means of var1 for two different categorical var2. But I'd like to do this directly, without running proc means beforehand, and without rounding the means and assigning it by myself.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2019 13:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571390#M161172</guid>
      <dc:creator>ellenqi</dc:creator>
      <dc:date>2019-07-05T13:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: create a new variable based on 2 other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571427#M161184</link>
      <description>&lt;P&gt;Are you familiar with DoW loops? That's primarily the way to handle this without PROC MEANs, but it's definitely an advanced topic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is there the limitation on PROC MEANS?&lt;/P&gt;
&lt;P&gt;What about SQL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280006"&gt;@ellenqi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hi everyone,&lt;/P&gt;
&lt;P&gt;here is my data&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;var1 var2&amp;nbsp; &amp;nbsp; newvar&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;23&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;12&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;14&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp;33&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp; 18&amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;7&amp;nbsp; &amp;nbsp; 45&amp;nbsp; &amp;nbsp; 3&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;Var1 is a continuous variable, and Var2 is a categorical variable (3 categories). For 1st category (var2=1), the newvar= mean of var 1 among the people in the 1st category. For the 2nd category (var2=2), the newvar=mean of var2 among the people in the 2nd category. For the 3rd category, the I just want to newvar=var1; Therefore the newvar will be assigned with the same value for 1st category, and with another same value for 2nd category.&lt;/P&gt;
&lt;P&gt;I know I can do this by running proc means to calculate the means of var1 for two different categorical var2. But I'd like to do this directly, without running proc means beforehand, and without rounding the means and assigning it by myself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2019 15:13:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571427#M161184</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-05T15:13:11Z</dc:date>
    </item>
    <item>
      <title>Re: create a new variable based on 2 other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571442#M161193</link>
      <description>&lt;P&gt;In the second category, the mean of var2 can only be 2 by definition (after conversion to numeric). As I don't think this is what you want, you should review your question.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280006"&gt;@ellenqi&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hi everyone,&lt;/P&gt;
&lt;P&gt;here is my data&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;var1 var2&amp;nbsp; &amp;nbsp; newvar&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp;23&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;12&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;14&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp;33&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp; 18&amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp;2&lt;/P&gt;
&lt;P&gt;7&amp;nbsp; &amp;nbsp; 45&amp;nbsp; &amp;nbsp; 3&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;Var1 is a continuous variable, and Var2 is a categorical variable (3 categories). For 1st category (var2=1), the newvar= mean of var 1 among the people in the 1st category. For the 2nd category (var2=2), the newvar=mean of var2 among the people in the 2nd category. For the 3rd category, the I just want to newvar=var1; Therefore the newvar will be assigned with the same value for 1st category, and with another same value for 2nd category.&lt;/P&gt;
&lt;P&gt;I know I can do this by running proc means to calculate the means of var1 for two different categorical var2. But I'd like to do this directly, without running proc means beforehand, and without rounding the means and assigning it by myself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2019 15:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571442#M161193</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-05T15:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: create a new variable based on 2 other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571452#M161196</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;is right that this is a typical job for the DoW loop, however the latter requires explicit sorting by VAR2. If you don't mind sorting, then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                           
  input id var1 var2 ;                
  cards ;                             
1  23  1                              
2  12  1                              
3  14  2                              
4  33  1                              
5  18  3                              
6  20  2                              
7  45  3                              
run ;                                 
                                      
proc sort data = have out = havesort ;
  by var2 id ;                        
run ;                                 
                                      
data want_dow (drop = _:) ;           
  do _n_ = 1 by 1 until (last.var2) ; 
    set havesort (keep = var:) ;      
    by var2 ;                         
    _s = sum (_s, var1) ;             
  end ;                               
  newvar = divide (_s, _n_) ;         
  do _n_ = 1 to _n_ ;                 
    set havesort ;                    
    output ;                          
  end ;                               
run ;                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to preserve the original data order without sorting forth and back, you can first aggregate the sums and counts using the hash object, then look it up for each record from&amp;nbsp; HAVE and compute the means:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_hash (drop = _:) ;                       
  if _n_ = 1 then do ;                             
    dcl hash h () ;                                
    h.definekey ("var2") ;                         
    h.definedata ("_s", "_q") ;                    
    h.definedone () ;                              
    do until (z) ;                                 
      set have (keep = var:) end = z ;             
      if h.find() ne 0 then call missing (_s, _q) ;
      _s = sum (_s, var1) ;                        
      _q = sum (_q, 1) ;                           
      h.replace() ;                                
    end ;                                          
  end ;                                            
  set have ;                                       
  h.find() ;                                       
  newvar = divide (_s, _q) ;                       
run ;                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, perhaps the simplest way (also suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;) is just to use the self-merging property of SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                                   
  create table want_sql as                   
  select id, var2, var1, avg (var1) as newvar
  from   have                                
  group  var2                                
  order  var2, id                            
  ;                                          
quit ;                                       
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Jul 2019 17:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/create-a-new-variable-based-on-2-other-variables/m-p/571452#M161196</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-05T17:09:08Z</dc:date>
    </item>
  </channel>
</rss>

