<?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: How can I write to a column specified by the value of another column? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816455#M322264</link>
    <description>&lt;P&gt;The E modifier on FINDW() is interesting.&amp;nbsp; Other useful ones for this might be I, R and perhaps S.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.foo;
   input Bin $ A1 B2;
datalines4;
A1	10	8
A1	10	8
B2	10	8
B2	10	8
A1	10	8
;;;;

proc sql noprint;
  select distinct bin into :bins separated by ' '
   from foo
  ;
quit;

data bar ;
  set foo ;
  array bins &amp;amp;bins;
  _n_ = findw("&amp;amp;bins",bin,' ','sire');
  if 0&amp;lt;_n_&amp;lt;=dim(bins) then bins[_n_]=max(0,bins[_n_]-1);
run;

proc compare data=foo compare=bar;
 id bin;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                 Value Comparison Results for Variables

       __________________________________________________________
                  ||       Base    Compare
        Bin       ||         A1         A1      Diff.     % Diff
        ________  ||  _________  _________  _________  _________
                  ||
        A1        ||    10.0000     9.0000    -1.0000   -10.0000
        A1        ||    10.0000     9.0000    -1.0000   -10.0000
        A1        ||    10.0000     9.0000    -1.0000   -10.0000
       __________________________________________________________


       __________________________________________________________
                  ||       Base    Compare
        Bin       ||         B2         B2      Diff.     % Diff
        ________  ||  _________  _________  _________  _________
                  ||
        B2        ||     8.0000     7.0000    -1.0000   -12.5000
        B2        ||     8.0000     7.0000    -1.0000   -12.5000
       __________________________________________________________
&lt;/PRE&gt;</description>
    <pubDate>Fri, 03 Jun 2022 21:34:29 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-06-03T21:34:29Z</dc:date>
    <item>
      <title>How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816415#M322244</link>
      <description>&lt;P&gt;So I have a data set of "bins" and quantities.&amp;nbsp; There are many bins, but below should be representative enough.&amp;nbsp; Two bins named "A1" and "B2", each with an initial capacity of 10 and 8 respectively.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data work.foo;
infile datalines dsd delimiter="	";
length Bin $2 A1 B2 8;
input Bin $ A1 B2;
datalines;
A1	10	8
A1	10	8
B2	10	8
B2	10	8
A1	10	8
;;;;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'd like to achieve is for each row decrement the column that is named in the "bin" variable.&lt;/P&gt;&lt;P&gt;Obviously some if-statements would solve this trivial case, but with a large number of column names (which may change) I'd like to avoid that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I discovered that I can reference the values using something like the vvaluex function (e.g. dynamically select a column to "pull" data from) but I was wondering if there was a similar capability to write to a column specified by the value of another column (e.g. dynamically select a column to "push" data to).&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;if vvaluex(bin) gt 0&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My desired output would be something like the table below:&lt;/P&gt;&lt;P&gt;Bin A1 B2&lt;BR /&gt;A1 9 8&lt;BR /&gt;A1 8 8&lt;BR /&gt;B2 8 7&lt;BR /&gt;B2 8 6&lt;BR /&gt;A1 7 6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 20:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816415#M322244</guid>
      <dc:creator>fergieis</dc:creator>
      <dc:date>2022-06-03T20:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816443#M322253</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.foo;
   infile datalines dsd delimiter="	";
   length Bin $2 A1 B2 8;
   input Bin $ A1 B2;
   array b[2] A1 B2;
   array d[2] _A1 _B2;
   if _n_ = 1 then do;
      _a1=A1; _b2=B2;
      end;
   retain d;
   do i = 1 to dim(b);
      if vname(b[i]) eq bin then do;
         d[i] + -1; leave;
         end;
      end;
   drop i;
   datalines;
A1	10	8
A1	10	8
B2	10	8
B2	10	8
A1	10	8
;;;;
run;
proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Added LEAVE statement.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 20:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816443#M322253</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2022-06-03T20:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816448#M322257</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/179981"&gt;@fergieis&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/179981"&gt;@fergieis&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;LI-CODE lang="sas"&gt;if vvaluex(bin) gt 0&lt;/LI-CODE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;To ensure that the values don't drop below zero you can use the MAX function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let binvars=A1 B2;

data want;
if _n_=1 then set foo(obs=1);
set foo(keep=bin);
array bins[*] &amp;amp;binvars;
_n_=findw("&amp;amp;binvars", trim(bin), " ", "e");
bins[_n_]=max(0,bins[_n_]-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are ways to create the macro variable containing the list of variable names without hardcoding (e.g., using PROC SQL).&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 21:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816448#M322257</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-06-03T21:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816455#M322264</link>
      <description>&lt;P&gt;The E modifier on FINDW() is interesting.&amp;nbsp; Other useful ones for this might be I, R and perhaps S.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.foo;
   input Bin $ A1 B2;
datalines4;
A1	10	8
A1	10	8
B2	10	8
B2	10	8
A1	10	8
;;;;

proc sql noprint;
  select distinct bin into :bins separated by ' '
   from foo
  ;
quit;

data bar ;
  set foo ;
  array bins &amp;amp;bins;
  _n_ = findw("&amp;amp;bins",bin,' ','sire');
  if 0&amp;lt;_n_&amp;lt;=dim(bins) then bins[_n_]=max(0,bins[_n_]-1);
run;

proc compare data=foo compare=bar;
 id bin;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                 Value Comparison Results for Variables

       __________________________________________________________
                  ||       Base    Compare
        Bin       ||         A1         A1      Diff.     % Diff
        ________  ||  _________  _________  _________  _________
                  ||
        A1        ||    10.0000     9.0000    -1.0000   -10.0000
        A1        ||    10.0000     9.0000    -1.0000   -10.0000
        A1        ||    10.0000     9.0000    -1.0000   -10.0000
       __________________________________________________________


       __________________________________________________________
                  ||       Base    Compare
        Bin       ||         B2         B2      Diff.     % Diff
        ________  ||  _________  _________  _________  _________
                  ||
        B2        ||     8.0000     7.0000    -1.0000   -12.5000
        B2        ||     8.0000     7.0000    -1.0000   -12.5000
       __________________________________________________________
&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jun 2022 21:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816455#M322264</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-03T21:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816463#M322272</link>
      <description>This definitely looks promising, and I already have the columns in a dataset, so I can use a select into.</description>
      <pubDate>Fri, 03 Jun 2022 22:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816463#M322272</guid>
      <dc:creator>fergieis</dc:creator>
      <dc:date>2022-06-03T22:12:57Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816466#M322275</link>
      <description>&lt;P&gt;Establish an array for all the variables that might be identified in BIN, to refer to the variable values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then initialize a second array of character values, containing the corresponding variable names.&amp;nbsp; Use whichc to identify which element of the variable-name array is in BIN, and decrement the corresponding values array element.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.foo;
   input Bin $ A1 B2;
datalines4;
A1	10	8
A1	10	8
B2	10	8
B2	10	8
A1	10	8
;;;;

data want (drop=i);
  set work.foo;
  array values{*} _numeric_;
  array vnames {100} $32 _temporary_;
  if _n_=1 then do i=1 to dim(values);
    vnames{i}=vname(values{i});
  end;
  values{whichc(bin,of vnames{*})}+(-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You have no more than 100 numeric variables in work.foo.&amp;nbsp; If you do then just make the VNAMES array larger.&lt;/LI&gt;
&lt;LI&gt;BIN will always have an actual variable name in it.&amp;nbsp; If BIN could possibly have a value that doesn't correspond to a variable name, then you might do something like&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  i=whichc(bin,of vnames{*});
  if i^=0 then values{i}+(-1);&lt;/CODE&gt;&lt;/PRE&gt;
instead of&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;values{whichc(bin,of vnames{*})}+(-1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 03 Jun 2022 22:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816466#M322275</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-06-03T22:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816492#M322293</link>
      <description>&lt;P&gt;A long dataset layout avoids complex array processing, andy you have the names readily available in a variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.foo;
infile datalines dlm="09"x dsd truncover;
input Bin $ A1 B2;
row = _n_;
datalines4;
A1	10	8
A1	10	8
B2	10	8
B2	10	8
A1	10	8
;;;;

proc transpose data=foo out=long1;
by row bin;
run;

proc sort data=long1;
by _name_ row;
run;

data long2;
set long1;
by _name_;
retain newcol;
if first._name_
then newcol = col1;
if bin = _name_ then newcol = max(newcol - 1,0);
run;

proc sort data=long2;
by row bin;
run;

proc transpose data=long2 out=want(drop=_name_);
by row bin;
var newcol;
id _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Depending on what you do next, you might even find that keeping the long layout has advantages.&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jun 2022 09:36:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816492#M322293</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-04T09:36:29Z</dc:date>
    </item>
    <item>
      <title>Re: How can I write to a column specified by the value of another column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816653#M322361</link>
      <description>&lt;P&gt;Tested everything out and works as advertised.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks very much for the assist.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2022 15:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-write-to-a-column-specified-by-the-value-of-another/m-p/816653#M322361</guid>
      <dc:creator>fergieis</dc:creator>
      <dc:date>2022-06-06T15:10:10Z</dc:date>
    </item>
  </channel>
</rss>

