<?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: Re-assign existing variable value in data set step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566282#M159113</link>
    <description>&lt;P&gt;TableName.VariableName (in red below) method of referring to variables is valid in SQL only, it is not valid in SAS data steps. In a data step you can only reference variables with their actual names. You can determine which records come from which data set using the IN data set option if desired.&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/278011"&gt;@annie0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had a question about the data set step when I tried to re-assign new value to an existing variable. Below are 2 example codes. The second example codes work good and generate the data what we want. Somehow the first code cannot assign the value properly. And I would like to know the reason for why code 1 not working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code 1:&amp;nbsp; ( I want to assign the all the non missing var2 from data d to missing value variable var1 from data b. However, in the test data, only the first non missing var2 from data d assigned to all the var1 missing value records.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;BR /&gt;if &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;b.var1&lt;/STRONG&gt;&lt;/FONT&gt;='' and &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;d.var2&lt;/STRONG&gt;&lt;/FONT&gt;^='' then &lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;var1=var2&lt;/STRONG&gt;&lt;/FONT&gt;; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code 2: (I separate the same code into 2 data step and the value can be assigned properly)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set test;&lt;BR /&gt;if var1='' and var2^='' then var1=var2; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jun 2019 20:30:31 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-14T20:30:31Z</dc:date>
    <item>
      <title>Re-assign existing variable value in data set step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566275#M159108</link>
      <description>&lt;P&gt;Hi SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had a question about the data set step when I tried to re-assign new value to an existing variable. Below are 2 example codes. The second example codes work good and generate the data what we want. Somehow the first code cannot assign the value properly. And I would like to know the reason for why code 1 not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example code 1:&amp;nbsp; ( I want to assign the all the non missing var2 from data d to missing value variable var1 from data b. However, in the test data, only the first non missing var2 from data d assigned to all the var1 missing value records.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;BR /&gt;if b.var1='' and d.var2^='' then var1=var2; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example code 2: (I separate the same code into 2 data step and the value can be assigned properly)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set test;&lt;BR /&gt;if var1='' and var2^='' then var1=var2; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 20:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566275#M159108</guid>
      <dc:creator>annie0</dc:creator>
      <dc:date>2019-06-14T20:00:36Z</dc:date>
    </item>
    <item>
      <title>Re: Re-assign existing variable value in data set step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566279#M159110</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/278011"&gt;@annie0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had a question about the data set step when I tried to re-assign new value to an existing variable. Below are 2 example codes. The second example codes work good and generate the data what we want. Somehow the first code cannot assign the value properly. And I would like to know the reason for why code 1 not working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code 1:&amp;nbsp; ( I want to assign the all the non missing var2 from data d to missing value variable var1 from data b. However, in the test data, only the first non missing var2 from data d assigned to all the var1 missing value records.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;BR /&gt;if b.var1='' and d.var2^='' then var1=var2; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Using b.var1 and d.var2 is is not valid data step code. Variable names can not have a dot in the name. Probably you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if var1='' and var2^='' then var1=var2;&lt;/CODE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 20:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566279#M159110</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-14T20:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: Re-assign existing variable value in data set step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566282#M159113</link>
      <description>&lt;P&gt;TableName.VariableName (in red below) method of referring to variables is valid in SQL only, it is not valid in SAS data steps. In a data step you can only reference variables with their actual names. You can determine which records come from which data set using the IN data set option if desired.&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/278011"&gt;@annie0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I had a question about the data set step when I tried to re-assign new value to an existing variable. Below are 2 example codes. The second example codes work good and generate the data what we want. Somehow the first code cannot assign the value properly. And I would like to know the reason for why code 1 not working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code 1:&amp;nbsp; ( I want to assign the all the non missing var2 from data d to missing value variable var1 from data b. However, in the test data, only the first non missing var2 from data d assigned to all the var1 missing value records.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;BR /&gt;if &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;b.var1&lt;/STRONG&gt;&lt;/FONT&gt;='' and &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;d.var2&lt;/STRONG&gt;&lt;/FONT&gt;^='' then &lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;var1=var2&lt;/STRONG&gt;&lt;/FONT&gt;; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example code 2: (I separate the same code into 2 data step and the value can be assigned properly)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set a b&amp;nbsp; c d;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;set test;&lt;BR /&gt;if var1='' and var2^='' then var1=var2; (variable 1 come from data b, while variable 2 come from data d)&lt;BR /&gt;IF var1^='';&lt;BR /&gt;run;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 20:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566282#M159113</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-14T20:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Re-assign existing variable value in data set step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566284#M159115</link>
      <description>&lt;P&gt;do you have a variable in the a b c and d datasets like an ID that the datasets share?&lt;/P&gt;
&lt;P&gt;otherwise your just stacking the data in examples 1 and 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 20:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566284#M159115</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-06-14T20:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: Re-assign existing variable value in data set step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566285#M159116</link>
      <description>&lt;P&gt;You may need to clear up your concept of what happens when you list multiple data sets in a SET statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set a b c d;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This takes the observations from each data set, separately (all from a, then all from b, then all from c, then all from d).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are no observations that come from both data set b and data set d.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you spell out a few observations from each data set, plus the result you are hoping to obtain, you can get suggestions about what the program should look like.&amp;nbsp; I suspect you need:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set a b c d (rename=(var2=var1));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the RENAME here applies only to data set d observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result your program produces actually requires fairly complex logic to understand.&amp;nbsp; Variables that come from a SAS data set are automatically retained.&amp;nbsp; So when data set D contains VAR2 (but not VAR1), your program changes VAR1 on the first observation from data set D.&amp;nbsp; Because data set B contains VAR1, the value is retained.&amp;nbsp; It just sits there when SAS encounters the second observation from data set D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 20:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566285#M159116</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-06-14T20:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: Re-assign existing variable value in data set step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566288#M159118</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
	select a.*, b.*, c.*, d.*
	from a as a,
	     b as b,
	     c as c,
	     d as d
	case when b.var1 = null and d.var2 ^= null then b.var1 = d.var2
	end
	order by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Jun 2019 20:52:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Re-assign-existing-variable-value-in-data-set-step/m-p/566288#M159118</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-06-14T20:52:12Z</dc:date>
    </item>
  </channel>
</rss>

