<?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: Fill in the value of a new variable based on the content of another one ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19526#M3045</link>
    <description>I am not sure whether the following code can work for SAS8, I have not used it before.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data start;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input a b c d $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1    2    3    a&lt;BR /&gt;
2    4    6    c&lt;BR /&gt;
3    6    9    b&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data start;&lt;BR /&gt;
 set start;&lt;BR /&gt;
 array var{*} a--c ; *a is first variable, b is last variable;&lt;BR /&gt;
 do i=1 to dim(var);&lt;BR /&gt;
  if vname(var{i}) eq d then e=var{i};&lt;BR /&gt;
 end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
    <pubDate>Thu, 03 Mar 2011 02:17:40 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2011-03-03T02:17:40Z</dc:date>
    <item>
      <title>Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19518#M3037</link>
      <description>Dear experts,&lt;BR /&gt;
&lt;BR /&gt;
I would like to be able to create a new variable in a dataset based on the content of another one. Let's say I have a dataset 'Test' which contains three variables (i.e. A, B and C) containing numeric values. A fourth variable (i.e. D) contains, as character value, the name of one of the three other variables (thus it can contain 'A', 'B' or 'C'). I would like being able to create a fifth variable (i.e. E) containing the value of the variable refered by the variable D... knowing that the variable referenced in column D could be different for each single observation of my dataset...&lt;BR /&gt;
&lt;BR /&gt;
By executing the following piece of code and having a look at the content of the dataset, you should better understand what I'm trying to do... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
data test(drop=i);&lt;BR /&gt;
	do i=1 to 3;&lt;BR /&gt;
		a= 1 * i;&lt;BR /&gt;
		b= 2 * i;&lt;BR /&gt;
		c= 3 * i;&lt;BR /&gt;
&lt;BR /&gt;
		if i = 1 then d= 'a'; else&lt;BR /&gt;
		if i = 2 then d= 'c'; else&lt;BR /&gt;
		if i = 3 then d= 'b';&lt;BR /&gt;
&lt;BR /&gt;
		output;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
I first thought to implement this by mean of the CALL EXECUTE but I would also like to know if you could come with another (and possibly more efficient) solution.&lt;BR /&gt;
&lt;BR /&gt;
Thank you in advance for your help !&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Florent

Subject edited&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: Florent</description>
      <pubDate>Tue, 01 Mar 2011 19:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19518#M3037</guid>
      <dc:creator>Florent</dc:creator>
      <dc:date>2011-03-01T19:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19519#M3038</link>
      <description>Check out the &lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002233818.htm" target="_blank"&gt;VVALUEX&lt;/A&gt; function.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
VVALUEX accepts a character expression as an argument. The function then&lt;BR /&gt;
evaluates the expression to determine the variable name and returns the value that &lt;BR /&gt;
is associated with that variable name.[/pre]</description>
      <pubDate>Tue, 01 Mar 2011 20:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19519#M3038</guid>
      <dc:creator>Tim_SAS</dc:creator>
      <dc:date>2011-03-01T20:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19520#M3039</link>
      <description>Hi Tim,&lt;BR /&gt;
&lt;BR /&gt;
Thank you for the very quick answer: it's exactly what I was looking for  ! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
&lt;BR /&gt;
Do you know whether there is a similar function available for SAS 8 ? My application is executing remotely on SAS 9.2 (so, no problem) but if the server is down or the connection can not be established then it's running on the users' local session (where SAS 8 - release 8.02 is installed...).&lt;BR /&gt;
&lt;BR /&gt;
Thanks again for your help !&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Florent</description>
      <pubDate>Tue, 01 Mar 2011 20:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19520#M3039</guid>
      <dc:creator>Florent</dc:creator>
      <dc:date>2011-03-01T20:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19521#M3040</link>
      <description>The SAS 8.2 doc is also &lt;A href="http://v8doc.sas.com/sashtml/" target="_blank"&gt;online here&lt;/A&gt;. The VVALUEX function is not included in the list of functions. &lt;BR /&gt;
&lt;BR /&gt;
Also, &lt;A href="http://support.sas.com/rnd/base/datastep/dsv9-sugi-v3.pdf" target="_blank"&gt;this 2004 SUGI paper&lt;/A&gt; says that VVALUEX was new for SAS 9.&lt;BR /&gt;
&lt;BR /&gt;
Sorry.</description>
      <pubDate>Tue, 01 Mar 2011 20:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19521#M3040</guid>
      <dc:creator>Tim_SAS</dc:creator>
      <dc:date>2011-03-01T20:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19522#M3041</link>
      <description>Hi:&lt;BR /&gt;
 It seems like this is a job for VVALUEX???? Assuming you have A, B, C, D already in existence.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
data start;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input a b c d $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1    2    3    a&lt;BR /&gt;
2    4    6    c&lt;BR /&gt;
3    6    9    b&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                              &lt;BR /&gt;
data make_E;&lt;BR /&gt;
  set start;&lt;BR /&gt;
  e = vvaluex(d);&lt;BR /&gt;
run;&lt;BR /&gt;
                     &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=make_E;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 01 Mar 2011 20:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19522#M3041</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2011-03-01T20:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19523#M3042</link>
      <description>Hi.&lt;BR /&gt;
How about this:&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
data start;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input a b c d $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1    2    3    a&lt;BR /&gt;
2    4    6    c&lt;BR /&gt;
3    6    9    b&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                              &lt;BR /&gt;
data make_E;&lt;BR /&gt;
  set start;&lt;BR /&gt;
  length e $ 10;&lt;BR /&gt;
  e=d;&lt;BR /&gt;
  e = tranwrd(e,'a',put(a,best4.));&lt;BR /&gt;
  e = tranwrd(e,'b',put(b,best4.));&lt;BR /&gt;
  e = tranwrd(e,'c',put(c,best4.));&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 02 Mar 2011 05:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19523#M3042</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-03-02T05:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19524#M3043</link>
      <description>@ Cynthia: This is indeed something for VVALUEX function but unfortunately this function was not available on SAS 8 (as mentioned by Tim).&lt;BR /&gt;
&lt;BR /&gt;
@Ksharp: your code does generate the expected result but requires that we update the code everytime we have a new value coming in (e.g. we add variable F and variable can reference to this new variable).&lt;BR /&gt;
&lt;BR /&gt;
Again, thanks for your contributions !&lt;BR /&gt;
&lt;BR /&gt;
Regards,&lt;BR /&gt;
Florent</description>
      <pubDate>Wed, 02 Mar 2011 11:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19524#M3043</guid>
      <dc:creator>Florent</dc:creator>
      <dc:date>2011-03-02T11:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19525#M3044</link>
      <description>I think these functions are available in version 8.  Does your example data fully represent the problem?&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data need;&lt;BR /&gt;
   set test;&lt;BR /&gt;
   array num&lt;LI&gt; _numeric_;&lt;BR /&gt;
   if VINARRAYX(d) then do;&lt;BR /&gt;
      do j = 1 to dim(num) until(vnameX(d) eq vname(num&lt;J&gt;)); end;&lt;BR /&gt;
      if j le dim(num) then d0 = num&lt;J&gt;;&lt;BR /&gt;
      end;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
Obs    a    b    c    d    j    d0&lt;BR /&gt;
&lt;BR /&gt;
 1     1    2    3    a    1     1&lt;BR /&gt;
 2     2    4    6    c    3     6&lt;BR /&gt;
 3     3    6    9    b    2     6&lt;BR /&gt;
[/pre]&lt;/J&gt;&lt;/J&gt;&lt;/LI&gt;</description>
      <pubDate>Wed, 02 Mar 2011 12:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19525#M3044</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2011-03-02T12:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: Fill in the value of a new variable based on the content of another one ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19526#M3045</link>
      <description>I am not sure whether the following code can work for SAS8, I have not used it before.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data start;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input a b c d $;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1    2    3    a&lt;BR /&gt;
2    4    6    c&lt;BR /&gt;
3    6    9    b&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data start;&lt;BR /&gt;
 set start;&lt;BR /&gt;
 array var{*} a--c ; *a is first variable, b is last variable;&lt;BR /&gt;
 do i=1 to dim(var);&lt;BR /&gt;
  if vname(var{i}) eq d then e=var{i};&lt;BR /&gt;
 end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 03 Mar 2011 02:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Fill-in-the-value-of-a-new-variable-based-on-the-content-of/m-p/19526#M3045</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-03-03T02:17:40Z</dc:date>
    </item>
  </channel>
</rss>

