<?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 to create and replace a variable shortly? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736574#M229487</link>
    <description>&lt;P&gt;Since you do not change the type, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
x1 = x1 + 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, if x1 was accidentally stored as character, then you would need the RENAME= dataset option, and INPUT function and a DROP:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (rename=(x1=x));
set have;
x1 = input(x,32.) + 2;
drop x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As long as the type is numeric, you can do it "in place", even if you want to convert value types:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
x1 = datetime();
format x1 e8601dt19.;
run;

data want;
set have;
x1 = datepart(x1);
format x1 yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 23 Apr 2021 10:23:04 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-04-23T10:23:04Z</dc:date>
    <item>
      <title>How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736515#M229446</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am wondering how to create and replace a variable totally?&lt;/P&gt;
&lt;P&gt;For example, I want to generate the variable x1 based on x, and I want to drop x, change the name of x1 to x&lt;/P&gt;
&lt;P&gt;My code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (rename=(x1=x));
set have;
x1=x+2;
drop x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am wondering if there is any other way or else to do so?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm regards.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 22:21:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736515#M229446</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-04-22T22:21:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736522#M229452</link>
      <description>&lt;P&gt;Rename x to something else when you do the SET command.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have (rename=(x=old_x));
x=old_x+2;
drop old_x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Apr 2021 22:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736522#M229452</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-22T22:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736524#M229453</link>
      <description>&lt;P&gt;If you are not changing the type just use the same variable on both sides of the =.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x=x+2;&lt;/P&gt;
&lt;P&gt;for example will replace a non-missing value of X with the value of X with 2 added to it.&lt;/P&gt;
&lt;P&gt;I specifically say "non-missing" because the result of adding 2 to a missing value is always 2. It is up to you to make sure the logic of the assignment meets the desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, this is one of the reasons we strongly suggest not using the way to0 common structure;&lt;/P&gt;
&lt;PRE&gt;data somename;
    set somename;
&amp;lt;code here&amp;gt;
;&lt;/PRE&gt;
&lt;P&gt;If you decide you need to do something else, possibly to a different variable or only for some values of x, all of the x's already had 2 added. So you would end up adding 2 again. And again. Every time you test a bit of logic or assignment code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 22:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736524#M229453</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-22T22:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736526#M229454</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your comment. In my case specifically, I just want to generate a new variable x1=x*1000. Is there any notice with multiple if using your approach, please? The example above I created to know the logical way to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is really nice that your way does not need a new variable being generated. I am still ambiguous about this part&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Also, this is one of the reasons we strongly suggest not using the way to0 common structure;&lt;/P&gt;
&lt;PRE&gt;data somename;
    set somename;
&amp;lt;code here&amp;gt;
;&lt;/PRE&gt;
&lt;P&gt;If you decide you need to do something else, possibly to a different variable or only for some values of x, all of the x's already had 2 added. So you would end up adding 2 again. And again. Every time you test a bit of logic or assignment code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks and warmest regards,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 22 Apr 2021 22:58:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736526#M229454</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-04-22T22:58:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736527#M229455</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your answer,&lt;/P&gt;
&lt;P&gt;But I do not why we need to rename on SET command rather than DATA statement. I did a test with data sashelp.class as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class1;
	set sashelp.class;
run;

data class2 (rename=(Age=birth));
	set class1;
run;

data class3;
	set class1 (rename=(Age=birth));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The datasets &lt;STRONG&gt;class2&lt;/STRONG&gt; and &lt;STRONG&gt;class3&lt;/STRONG&gt; all have the column &lt;STRONG&gt;birth&lt;/STRONG&gt; instead of &lt;STRONG&gt;Age&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm and thanks,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Apr 2021 23:04:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736527#M229455</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-04-22T23:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736542#M229460</link>
      <description>&lt;P&gt;If the variable is already numeric then just modify the VALUES and leave the name alone.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  x=x*1000;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the variable is character and you want to make a new numeric variable that uses the same name then you will have to use rename (and perhaps also drop).&amp;nbsp; But there is no need to use dataset options on either the input or output dataset referece.&lt;/P&gt;
&lt;P&gt;Just use the normal data step commands.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  new_x = input(x,32.)*1000;
  rename new_x=x;
  drop x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 04:11:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736542#M229460</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-04-23T04:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736573#M229486</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your answer,&lt;/P&gt;
&lt;P&gt;But I do not why we need to rename on SET command rather than DATA statement. I did a test with data sashelp.class as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class1;
	set sashelp.class;
run;

data class2 (rename=(Age=birth));
	set class1;
run;

data class3;
	set class1 (rename=(Age=birth));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The datasets &lt;STRONG&gt;class2&lt;/STRONG&gt; and &lt;STRONG&gt;class3&lt;/STRONG&gt; all have the column &lt;STRONG&gt;birth&lt;/STRONG&gt; instead of &lt;STRONG&gt;Age&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm and thanks,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This isn't the same example. You are not replacing a variable with a given name with a new variable of the same name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But anyway, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; have better solutions.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 10:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736573#M229486</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-23T10:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736574#M229487</link>
      <description>&lt;P&gt;Since you do not change the type, do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
x1 = x1 + 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, if x1 was accidentally stored as character, then you would need the RENAME= dataset option, and INPUT function and a DROP:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (rename=(x1=x));
set have;
x1 = input(x,32.) + 2;
drop x;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As long as the type is numeric, you can do it "in place", even if you want to convert value types:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
x1 = datetime();
format x1 e8601dt19.;
run;

data want;
set have;
x1 = datepart(x1);
format x1 yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Apr 2021 10:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736574#M229487</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-23T10:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to create and replace a variable shortly?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736596#M229502</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/212695"&gt;@Phil_NZ&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your answer,&lt;/P&gt;
&lt;P&gt;But I do not why we need to rename on SET command rather than DATA statement. I did a test with data sashelp.class as below:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class1;
	set sashelp.class;
run;

data class2 (rename=(Age=birth));
	set class1;
run;

data class3;
	set class1 (rename=(Age=birth));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The datasets &lt;STRONG&gt;class2&lt;/STRONG&gt; and &lt;STRONG&gt;class3&lt;/STRONG&gt; all have the column &lt;STRONG&gt;birth&lt;/STRONG&gt; instead of &lt;STRONG&gt;Age&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm and thanks,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Where you place the rename, data set option on set statement, data set option on data statement , or a simple rename statement not as an option depends on why you are renaming at all.&lt;/P&gt;
&lt;P&gt;This also does the same as your simple examples:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;data class4;
   set class1;
   rename Age=Birth;
run;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sometimes you rename on a set statement because you are "setting" multiple data sets and two variables that need to be treated the same are set to the same name to make the coding "nicer"&lt;/P&gt;
&lt;PRE&gt;data want; 
     set dataone (rename=(value2020 = value) )
         datatwo (rename=(value2021 = value) )
         datathr (rename=(value2019 = value) )
    ;
    &amp;lt;code that uses VALUE instead of bodging 3 variable names&amp;gt;
;&lt;/PRE&gt;
&lt;P&gt;This happens quite often when you get data from multiple contributors or someone has lousy variable naming practices and places data, such as the year into a variable name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also be creating two or more data sets and for some reason need the names in the output data sets to be different. That would be a case where the rename needs to be a data set option for each output set.&lt;/P&gt;
&lt;P&gt;Sometimes it is just style choice if you don't manipulate the renamed variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some people will use a data step to rename a variable without realizing that SAS has a procedure, DATASETS, that will change properties (name, format, label, informat) in place (not values). Which can be much quicker than a data step because when you use a data step every record is processed, which means moved through the data vector, and can take a long time for large data sets. Proc Datasets manipulated the values in the data set header.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Apr 2021 14:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-and-replace-a-variable-shortly/m-p/736596#M229502</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-23T14:52:38Z</dc:date>
    </item>
  </channel>
</rss>

