<?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: Adding up variables in one dataset based on a second dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603763#M174953</link>
    <description>&lt;P&gt;Thank you for contributing this code! it works really well!&lt;/P&gt;</description>
    <pubDate>Wed, 13 Nov 2019 05:03:39 GMT</pubDate>
    <dc:creator>tonybesas</dc:creator>
    <dc:date>2019-11-13T05:03:39Z</dc:date>
    <item>
      <title>Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603576#M174873</link>
      <description>&lt;P&gt;I have this primary dataset that looks like this, with thousands of lines and hundreds of columns:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;file   baby back ball bear beat begin call
text1  3    4    0    1    0    1     0
text2  2    0    0    3    3    1     2
text3  0    0    0    1    0    5     1
text4  1    1    2    1    1    4     1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And I also have this secondary dataset that looks like this, which spells out the variables in the primary dataset that should be added up:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;word pole
baby f3neg
back f1pos
ball f1pos
bear f2pos
beat f2neg
begin f2neg
call f3pos&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'd like to do is, for each value of 'file' in the primary database, to sum up the values for the words in the primary database according to the distribution of the words in the secondary database, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;F1=(sum of all f1pos words if any) - (sum of all f1neg words if any)&lt;BR /&gt;F2=(sum of all f2pos words if any) - (sum of all f2neg words if any)&lt;BR /&gt;F3=(sum of all f3pos words if any) - (sum of all f3neg words if any)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;F1 for text1 = ( 4 for back + 0 for ball ) - ( 0 because no words are marked as f1neg in the secondary database ) = 4&lt;BR /&gt;F1 for text2 = ( 0 for back + 0 for ball ) - ( 0 because no words are marked as f1neg in the secondary database ) = 0&lt;BR /&gt;F1 for text3 = ( 0 for back + 0 for ball ) - ( 0 because no words are marked as f1neg in the secondary database ) = 0&lt;BR /&gt;F1 for text4 = ( 1 for back + 2 for ball ) - ( 0 because no words are marked as f1neg in the secondary database ) = 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;F2 for text1 = ( 1 for bear ) - ( 0 for beat + 1 for begin ) = 0&lt;BR /&gt;F2 for text2 = ( 3 for bear ) - ( 3 for beat + 1 for begin ) = -1&lt;BR /&gt;F2 for text3 = ( 1 for bear ) - ( 0 for beat + 5 for begin ) = -4&lt;BR /&gt;F2 for text4 = ( 1 for bear ) - ( 1 for beat + 4 for begin ) = -4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on for F3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and then add this back to the primary database:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;file   baby back ball bear beat begin call  F1   F2   F3
text1  3    4    0    1    0    1     0     4     0   -3
text2  2    0    0    3    3    1     2     0    -1    0 
text3  0    0    0    1    0    5     1     0    -4    1
text4  1    1    2    1    1    4     1     3    -4    0

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How could this be done in SAS?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you ahead&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 13:30:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603576#M174873</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-12T13:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603580#M174875</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222515"&gt;@tonybesas&lt;/a&gt;&amp;nbsp;Hi and welcome to the SAS Community &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is one way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input file $ baby back ball bear beat begin call;
datalines;
text1 3 4 0 1 0 1 0
text2 2 0 0 3 3 1 2
text3 0 0 0 1 0 5 1
text4 1 1 2 1 1 4 1
;

data two;
input word $ pole $;
datalines;
baby f3neg
back f1pos
ball f1pos
bear f2pos
beat f2neg
begin f2neg
call f3pos
;

data want(drop=i v word pole);
    if _N_=1 then do;
        declare hash h(dataset:'two');
        h.definekey('word', 'pole');
        h.definedone();
    end;

    set one;
    if 0 then set two;
    array _ {*} _numeric_;
    
    do i=1 to dim(_);
        v=vname(_[i]);
        if h.check(key:v, key:'f1pos')=0 then F1=sum(F1,    _[i]);
        if h.check(key:v, key:'f1neg')=0 then F1=sum(F1, -1*_[i]);
        if h.check(key:v, key:'f2pos')=0 then F2=sum(F2,    _[i]);
        if h.check(key:v, key:'f2neg')=0 then F2=sum(F2, -1*_[i]);
        if h.check(key:v, key:'f3pos')=0 then F3=sum(F3,    _[i]);
        if h.check(key:v, key:'f3neg')=0 then F3=sum(F3, -1*_[i]);
    end;
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;file  baby back ball bear beat begin call F1  F2  F3
text1 3    4    0    1    0    1     0    4   0   -3
text2 2    0    0    3    3    1     2    0   -1  0
text3 0    0    0    1    0    5     1    0   -4  1
text4 1    1    2    1    1    4     1    3   -4  0&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 13:54:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603580#M174875</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-12T13:54:10Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603582#M174876</link>
      <description>&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dothis;
proc sql noprint;
    select distinct word into :f1pos separated by ',' from secondary_data_set
        where lowcase(pole)='f1pos';
    select distinct word into :f1neg separated by ',' from secondary_data_set
        where lowcase(pole)='f1neg';
    select distinct word into :f2pos separated by ',' from secondary_data_set
        where lowcase(pole)='f2pos';
    select distinct word into :f2neg separated by ',' from secondary_data_set
        where lowcase(pole)='f2neg';
    select distinct word into :f3pos separated by ',' from secondary_data_set
        where lowcase(pole)='f3pos';
    select distinct word into :f3neg separated by ',' from secondary_data_set
        where lowcase(pole)='f3neg';
quit;

data want;
    set have;
    f1=%if &amp;amp;f1pos^= %then sum(&amp;amp;f1pos); -
       %if &amp;amp;f1neg^= %then sum(&amp;amp;f1neg); ;
    /* you do the typing for F2 and F3, I'm lazy */
run;
%mend;

%dothis&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2019 14:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603582#M174876</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-12T14:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603601#M174887</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222515"&gt;@tonybesas&lt;/a&gt;&amp;nbsp; &amp;nbsp;First off, very interesting question from the perspective of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;1. How to build an algorithm that will scale&lt;/P&gt;
&lt;P&gt;&amp;nbsp;2. How to structure the flow&lt;/P&gt;
&lt;P&gt;&amp;nbsp;3. How to look up, rotate, look up and again rotate or in other words going from vertical to horizontal and vice versa.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following is simple data-driven and yet will scale. The positive and negatives are the only categorical component that matters in a series and the series can be any number.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data one;
input file $ baby back ball bear beat begin call;
datalines;
text1 3 4 0 1 0 1 0
text2 2 0 0 3 3 1 2
text3 0 0 0 1 0 5 1
text4 1 1 2 1 1 4 1
;

data two;
input word $ pole $;
datalines;
baby f3neg
back f1pos
ball f1pos
bear f2pos
beat f2neg
begin f2neg
call f3pos
;
proc transpose data=one out=t;
by file;
var baby--call;
run;
proc sql;
create table t2 as
select file,substr(pole,1,2) as series, substr(pole,3) as sign,sum(col1) as s
from t a left join two b
on a._name_=b.word
group by file,series,sign
order by file,series,sign;
quit;

data t3;
set t2;
by file series;
dif=dif(s);
if first.series then dif=s;
if last.series;
run;

proc transpose data=t3(keep=file series dif) out=want(drop=_name_);
by file ;
id series;
var dif;
run;

data final_want;
merge one want;
by file;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:12:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603601#M174887</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-12T15:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603605#M174890</link>
      <description>&lt;P&gt;I would format you metadata table to be easier to use for generating the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
  input source :$32. target :$32. sign :$1.;
datalines;
baby f3 -
back f1 +
ball f1 +
bear f2 +
beat f2 -
begin f2 -
call f3 +
;

proc sort data=two;
  by target source ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use that metadata to generate the statements you need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  file code ;
  set two ;
  by target ;
  if first.target then put target '=sum(' @;
  else put ',' @;
  put sign source @ ;
  if last.target then put ');' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resulting text file:&lt;/P&gt;
&lt;PRE&gt;f1 =sum(+ back ,+ ball );
f2 =sum(+ bear ,- beat ,- begin );
f3 =sum(- baby ,+ call );&lt;/PRE&gt;
&lt;P&gt;You can then use that code against your input data.&lt;/P&gt;
&lt;PRE&gt;38   data want;
39     set one ;
40     %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is file /Volumes/scratch/SAS_work3B6800005EBE_amrdrml464.pfizer.com/#LN00020.
41  +f1 =sum(+ back ,+ ball );
42  +f2 =sum(+ bear ,- beat ,- begin );
43  +f3 =sum(- baby ,+ call );
NOTE: %INCLUDE (level 1) ending.
44   run;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    file     baby    back    ball    bear    beat    begin    call    f1    f2    f3

 1     text1      3       4       0       1       0       1        0      4     0    -3
 2     text2      2       0       0       3       3       1        2      0    -1     0
 3     text3      0       0       0       1       0       5        1      0    -4     1
 4     text4      1       1       2       1       1       4        1      3    -4     0&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 15:31:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603605#M174890</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-12T15:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603609#M174892</link>
      <description>&lt;P&gt;If you have SAS/STAT licensed then use PROC SCORE.&amp;nbsp; Your metadata needs to be in the format of a scoring dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Obs    _type_    _name_    back    ball    bear    beat    begin    baby    call

 1     SCORE       f1        1       1       .       .        .       .       .
 2     SCORE       f2        .       .       1      -1       -1       .       .
 3     SCORE       f3        .       .       .       .        .      -1       1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can use it with PROC SCORE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc score data=one score=score out=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can convert your existing layout with just a little coding.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
  input source :$32. target :$32. factor ;
datalines;
baby f3 -1
back f1 1
ball f1 1
bear f2 1
beat f2 -1
begin f2 -1
call f3 1
;

proc sort data=two;
  by target ;
run;
proc transpose data=two out=score ;
  by target ;
  id source;
  var factor;
run;
data score;
  _type_='SCORE';
  set score;
  drop _name_;
  rename target=_name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2019 16:04:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603609#M174892</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-12T16:04:51Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603626#M174900</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data one;
input file $ baby back ball bear beat begin call;
datalines;
text1 3 4 0 1 0 1 0
text2 2 0 0 3 3 1 2
text3 0 0 0 1 0 5 1
text4 1 1 2 1 1 4 1
;

data two;
input word $ pole $;
datalines;
baby f3neg
back f1pos
ball f1pos
bear f2pos
beat f2neg
begin f2neg
call f3pos
;

data temp;
 if _n_=1 then do;
  if 0 then set one two;
  dcl hash H (dataset:'two');
  h.definekey  ("word");
  h.definedata ("pole");
  h.definedone ();
  dcl hash H1 (ordered:'y');
  h1.definekey  ("pole");
  h1.definedata ("pole","s");
  h1.definedone ();
  dcl hiter hi('h1');
 end;
 set one;
 array t baby--call;
 do over t;
  rc=h.find(key:vname(t));
  if h1.find() ne 0 then s=t;
  else s=sum(s,t);
  h1.replace();
 end;
 do while(hi.next()=0);
  series=substr(pole,1,2);
  output;
 end;
 h1.clear();
 keep file series  s;
run;

proc sql noprint;
select distinct series into :series separated by ' '
from w1;
quit;

data want;
 if _n_=1 then do;
 if 0 then set one ;
   dcl hash H (dataset:'one') ;
   h.definekey  ("file") ;
   h.definedata (all:'y') ;
   h.definedone () ; 
   array t(*) &amp;amp;series;
 end;
 do _n_=1 by 1 until(last.file);
  do until(last.series);
   set temp;
   by file series;
   dif=dif(s);
   if first.series then dif=s;
  end;
  h.find();
  t(_n_)=dif;
 end;
 drop series dif s;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Nov 2019 21:49:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603626#M174900</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-12T21:49:24Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603727#M174933</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222515"&gt;@tonybesas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Something like below should work - under the assumption that the order of WORD values in TWO matches that of the like-named variables in ONE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one ;                                                                                                                                                                                                                                                      
  input file $ baby back ball bear beat begin call ;                                                                                                                                                                                                            
  lines ;                                                                                                                                                                                                                                                       
text1 3 4 0 1 0 1 0                                                                                                                                                                                                                                             
text2 2 0 0 3 3 1 2                                                                                                                                                                                                                                             
text3 0 0 0 1 0 5 1                                                                                                                                                                                                                                             
text4 1 1 2 1 1 4 1                                                                                                                                                                                                                                             
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data two ;                                                                                                                                                                                                                                                      
  input word $ pole $ ;                                                                                                                                                                                                                                         
  lines ;                                                                                                                                                                                                                                                       
baby  f3neg                                                                                                                                                                                                                                                     
back  f1pos                                                                                                                                                                                                                                                     
ball  f1pos                                                                                                                                                                                                                                                     
bear  f2pos                                                                                                                                                                                                                                                     
beat  f2neg                                                                                                                                                                                                                                                     
begin f2neg                                                                                                                                                                                                                                                     
call  f3pos                                                                                                                                                                                                                                                     
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  array x [2,7] _temporary_ ;                                                                                                                                                                                                                                   
  if _n_ = 1 then do _i_ = 1 by 1 until (z) ;                                                                                                                                                                                                                   
    set two (keep = pole) end = z ;                                                                                                                                                                                                                             
    x[1,_i_] = input (char (pole, 2), 1.) ;                                                                                                                                                                                                                     
    x[2,_i_] = choosen (findc ("np", char (pole,3)), -1, 1) ;                                                                                                                                                                                                   
  end ;                                                                                                                                                                                                                                                         
  set one ;                                                                                                                                                                                                                                                     
  array v baby--call ;                                                                                                                                                                                                                                          
  array F [3] ;                                                                                                                                                                                                                                                 
  do over v ;                                                                                                                                                                                                                                                   
    _n_ = x[1,_i_] ;                                                                                                                                                                                                                                            
    f[_n_] = sum (f[_n_], v * x[2,_i_]) ;                                                                                                                                                                                                                       
  end ;                                                                                                                                                                                                                                                         
run ;                             
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, alternatively, the array X can be replaced by a hash table, which has the advantage of not making the assumption mentioned above, as the attributes for each variable (which F and which sign) are searched by the variable name in the table X:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop = _:) ;                                                                                                                                                                                                                                         
  if _n_ = 1 then do ;                                                                                                                                                                                                                                          
    dcl hash x () ;                                                                                                                                                                                                                                             
    x.definekey ("_w") ;                                                                                                                                                                                                                                        
    x.definedata ("_n_", "_iorc_") ;                                                                                                                                                                                                                            
    x.definedone () ;                                                                                                                                                                                                                                           
    do _n_ = 1 by 1 until (z) ;                                                                                                                                                                                                                                 
      set two (rename=word=_w) end = z ;                                                                                                                                                                                                                        
      x.add (key:_w, data:input(char(pole,2),1.), data:choosen(findc("np",char (pole,3)),-1,1)) ;                                                                                                                                                               
    end ;                                                                                                                                                                                                                                                       
  end ;                                                                                                                                                                                                                                                         
  set one ;                                                                                                                                                                                                                                                     
  array v baby--call ;                                                                                                                                                                                                                                          
  array F [3] ;                                                                                                                                                                                                                                                 
  do over v ;                                                                                                                                                                                                                                                   
    x.find (key:vname(v)) ;                                                                                                                                                                                                                                     
    f[_n_] = sum (f[_n_], v * _iorc_) ;                                                                                                                                                                                                                         
  end ;                                                                                                                                                                                                                                                         
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 23:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603727#M174933</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-12T23:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603761#M174951</link>
      <description>&lt;P&gt;This works great, really fast! Thank you for taking the time to do this, draycut!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 04:53:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603761#M174951</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T04:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603763#M174953</link>
      <description>&lt;P&gt;Thank you for contributing this code! it works really well!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 05:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603763#M174953</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T05:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603766#M174955</link>
      <description>&lt;P&gt;Great code, took care of the job! thank you, novinosrin,&lt;SPAN class="login-bold"&gt;&amp;nbsp;for taking the time to contribute!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 05:23:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603766#M174955</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T05:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603767#M174956</link>
      <description>&lt;DIV class="lia-quilt-row lia-quilt-row-forum-message-main"&gt;&lt;DIV class="lia-quilt-column lia-quilt-column-20 lia-quilt-column-right lia-quilt-column-main-right"&gt;&lt;DIV class="lia-quilt-column-alley lia-quilt-column-alley-right"&gt;&lt;DIV class="lia-message-body lia-component-body"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;Great code, took care of the job! thank you, novinosrin,&lt;SPAN class="login-bold"&gt;&amp;nbsp;for taking the time to contribute!&lt;/SPAN&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="lia-quilt-row lia-quilt-row-forum-message-footer"&gt;&lt;DIV class="lia-quilt-column lia-quilt-column-04 lia-quilt-column-left lia-quilt-column-footer-left"&gt;&lt;DIV class="lia-quilt-column-alley lia-quilt-column-alley-left"&gt;&lt;DIV class="lia-message-notify lia-component-report-abuse"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 13 Nov 2019 05:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603767#M174956</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T05:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603768#M174957</link>
      <description>&lt;P&gt;Sorry, I meant to reply to your first code, novinosrin, but somehow this ended up beneath your second code. It turns out, the second code generates an error message, pointing to the ampersand:&lt;/P&gt;&lt;DIV class="sasSource"&gt;array t(*) &amp;amp;series;&lt;/DIV&gt;&lt;DIV class="sasError"&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a numeric constant, $, (, ;, VARCHAR, _ALL_, _CHARACTER_,&lt;/DIV&gt;&lt;DIV class="sasError"&gt;_CHAR_, _NUMERIC_, _TEMPORARY_.&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 05:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603768#M174957</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T05:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603773#M174959</link>
      <description>&lt;P&gt;Thank you for taking the time to contribute the code, hashman! My larger dataset causes this error:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;ERROR: Array subscript out of range&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class="sasSource"&gt;_i_=8 z=0 pole=f1pos corpus= wrcount=. file= baby=. back=. ball=. bear=. beat=. begin=. believe=. bitch=. black=. block=. blow=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;blue=. body=. bottle=. bout=. brain=. break=. bring=. call=. care=. catch=. cause=. chance=. change=. check=. close=. cold=. come=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;cool=. crazy=. crime=. dance=. dark=. dead=. **bleep**=. doctor=. dollar=. door=. dream=. drink=. drive=. everything=. face=. fall=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;feel=. find=. fine=. fire=. first=. floor=. follow=. foot=. forget=. free=. friend=. front=. **bleep**=. full=. game=. girl=. give=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;good=. gotta=. great=. green=. ground=. grow=. guess=. hand=. happen=. happy=. hard=. hate=. head=. hear=. heart=. high=. hold=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;home=. hope=. house=. hurt=. keep=. kill=. kiss=. know=. lady=. last=. learn=. leave=. life=. light=. like=. line=. listen=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;little=. live=. long=. look=. lose=. love=. make=. many=. matter=. mean=. meet=. middle=. mind=. mine=. miss=. money=. more=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;morning=. mother=. motherf=. move=. much=. name=. neck=. need=. next=. night=. nobody=. nothing=. number=. only=. open=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;other=. part=. party=. peace=. people=. phone=. pill=. place=. play=. please=. problem=. pull=. radio=. rain=. read=. real=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;reason=. remember=. rest=. ride=. right=. rock=. roll=. round=. same=. search=. seem=. send=. sense=. shix=. shoot=. shot=. show=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;side=. sing=. single=. skin=. sleep=. smile=. somebody=. someone=. something=. song=. soul=. sound=. space=. speak=. spot=. stand=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;star=. start=. stay=. stick=. stop=. street=. strong=. take=. talk=. tear=. tell=. test=. thing=. think=. thought=. throw=. time=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;today=. touch=. town=. true=. turn=. understand=. voice=. wait=. walk=. wall=. wanna=. want=. watch=. whole=. wish=. woman=. word=.&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;work=. world=. wrong=. year=. young=. F1=. F2=. F3=. _ERROR_=1 _N_=1&lt;/DIV&gt;&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;for this line:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;x[1,_i_] = input (char (pole, 2), 1.) ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 06:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603773#M174959</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T06:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603776#M174962</link>
      <description>&lt;P&gt;Yes, I have PROC SCORE in my installation, and this works really well! Thank you, Tom, for your contribution!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 06:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603776#M174962</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T06:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603778#M174963</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/222515"&gt;@tonybesas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Well, you must've realized that 7 means the number of variables in question you showed in your input sample. Obviously, now you have more. So, just to account for (almost) all eventualities, instead of:&lt;/P&gt;
&lt;PRE&gt;array x [2,7] _temporary_ ; &lt;/PRE&gt;
&lt;P&gt;code:&lt;/P&gt;
&lt;PRE&gt;array x [2,999999] _temporary_ ; &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I doubt you can have more than 999999 variables in ONE, so it should cover the territory. All you risk to lose is extra 16 MB of memory. It would matter 25 years ago on the mainframe with REGION=256K. Nowadays, it's peanuts.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or just use the hash variant, as it's self-adjusting to the number of variables in ONE and records in TWO, plus it doesn't care whether the orders of the variable names in ONE and values of WORD in TWO match.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 06:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603778#M174963</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-13T06:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603781#M174965</link>
      <description>Thank you, Paul, I'll try this!&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Nov 2019 06:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603781#M174965</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T06:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603815#M174977</link>
      <description>&lt;P&gt;hashman kindly posted a replacement for the line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; &amp;nbsp;array x [2,7] _temporary_ ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which is this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; &amp;nbsp;array x [2,99999] _temporary_ ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this accommodates a much larger number of variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the revised code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ; 
&amp;nbsp; &amp;nbsp;array x [2,99999] _temporary_ ; 
&amp;nbsp; &amp;nbsp;if _n_ = 1 then do _i_ = 1 by 1 until (z) ; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;set two (keep = pole) end = z ; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x[1,_i_] = input (char (pole, 2), 1.) ; 
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x[2,_i_] = choosen (findc ("np", char (pole,3)), -1, 1) ; 
&amp;nbsp; end ; 
&amp;nbsp; set one ; 
&amp;nbsp; array v baby--young ; 
&amp;nbsp; array F [3] ; 
&amp;nbsp; do over v ; 
&amp;nbsp; &amp;nbsp; &amp;nbsp;_n_ = x[1,_i_] ; 
&amp;nbsp; &amp;nbsp; &amp;nbsp;f[_n_] = sum (f[_n_], v * x[2,_i_]) ; 
&amp;nbsp; end ;
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this worked really well!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 11:32:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603815#M174977</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T11:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: Adding up variables in one dataset based on a second dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603817#M174978</link>
      <description>&lt;P&gt;thank you, Tom, this worked really well!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 11:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Adding-up-variables-in-one-dataset-based-on-a-second-dataset/m-p/603817#M174978</guid>
      <dc:creator>tonybesas</dc:creator>
      <dc:date>2019-11-13T11:35:40Z</dc:date>
    </item>
  </channel>
</rss>

