<?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: Creating multiple new variables from multiple existing variables in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539879#M7140</link>
    <description>&lt;P&gt;Someone will suggest a way to do this in SAS, but thinking outside the box...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create lines of SAS code in Excel, simply by concatenating strings. One you have a column of text with the code you need, simply copy the cells from Excel, and paste them into SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result isn't "pretty", but it does work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh, and welcome to the forum!&lt;/P&gt;</description>
    <pubDate>Sun, 03 Mar 2019 08:00:01 GMT</pubDate>
    <dc:creator>Norman21</dc:creator>
    <dc:date>2019-03-03T08:00:01Z</dc:date>
    <item>
      <title>Creating multiple new variables from multiple existing variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539876#M7139</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am a new SAS user, and this is my first time posting a question. I did try to look for existing answers but I have not seen a similar question being asked. So maybe it's either not feasible or laughably simple. So please bare with me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table with a date variable, a "RF" (stands for "risk free") variable, and 20+ variables with these names:&lt;/P&gt;&lt;P&gt;small_lobm, me1_bm2, ,me1_bm3, me1_bm4, small_hibm,&amp;nbsp;&lt;/P&gt;&lt;P&gt;me2_bm1,&amp;nbsp;me2_bm2, ,me2_bm3, me2_bm4,&amp;nbsp;me2_bm5,&lt;/P&gt;&lt;P&gt;me3_bm1,&amp;nbsp;me3_bm2, ,me3_bm3, me3_bm4,&amp;nbsp;me3_bm5,&lt;/P&gt;&lt;P&gt;me4_bm1,&amp;nbsp;me4_bm2, ,me4_bm3, me4_bm4,&amp;nbsp;me4_bm5,&lt;/P&gt;&lt;P&gt;big_lobm,&amp;nbsp;me5_bm2, ,me5_bm3, me5_bm4, big_hibm&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I break them into 5 lines just for the ease of reading so you can see how the names were constructed). So what I am trying to do is, for each of these 20+ variables, create a new column that is equal to variable - RF. Something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
set have;
small_lobmx = small_lobm - RF;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I try to see if it's possible to not have to do that same thing 20+ times for each of them. And I don't need to keep the old 20+ ones afterward. I'm using AS 9.4. Please help and thank you very much!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Mar 2019 06:51:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539876#M7139</guid>
      <dc:creator>va003</dc:creator>
      <dc:date>2019-03-03T06:51:36Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple new variables from multiple existing variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539879#M7140</link>
      <description>&lt;P&gt;Someone will suggest a way to do this in SAS, but thinking outside the box...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create lines of SAS code in Excel, simply by concatenating strings. One you have a column of text with the code you need, simply copy the cells from Excel, and paste them into SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result isn't "pretty", but it does work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh, and welcome to the forum!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Mar 2019 08:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539879#M7140</guid>
      <dc:creator>Norman21</dc:creator>
      <dc:date>2019-03-03T08:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple new variables from multiple existing variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539880#M7141</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264876"&gt;@va003&lt;/a&gt;&amp;nbsp;and again&amp;nbsp;welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use arrays and a DO loop to perform the calculation "newvar = oldvar - RF" for all 20+ variables. While the list of the existing 20+ variables could be written as&amp;nbsp;&lt;SPAN&gt;&lt;FONT face="courier new,courier"&gt;small_lobm--big_hibm&lt;/FONT&gt;&amp;nbsp;(assuming that&amp;nbsp;&lt;FONT face="courier new,courier"&gt;small_lobm&lt;/FONT&gt; is the first and&amp;nbsp;&lt;FONT face="courier new,courier"&gt;big_hibm&lt;/FONT&gt;&amp;nbsp;is the last of the 20+ variables in the dataset), we need another technique to construct the new variable names (with suffix "x"). One possible way to do this is to retrieve the variable names from dictionary table COLUMNS.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The PROC SQL step below creates a macro variable NEWNAMES containing the list of the 20+ new variable names, separated by blanks. For convenience, it also&amp;nbsp;creates a macro variable OLDNAMES containing the list of the 20+ old variable names (so we don't need to use the list &lt;FONT face="courier new,courier"&gt;small_lobm--big_hibm&lt;/FONT&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select name, cats(name,'x') into :oldnames separated by ' ',
                                 :newnames separated by ' '
from dictionary.columns
where libname='WORK' &amp;amp; memname='HAVE' &amp;amp; upcase(name) not in ('DATE' 'RF')
order by varnum;
quit;

data want;
set have;
array v [*] &amp;amp;oldnames;
array vx[*] &amp;amp;newnames;
do i=1 to dim(v);
  vx[i]=v[i]-rf;
end;
drop i &amp;amp;oldnames; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;When you adapt the above code to your real dataset, make sure to:&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;replace "HAVE" by the name of your dataset (upper case!) in the WHERE clause of the PROC SQL step&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;replace "WORK" by the name of the library where it resides (upper case!)&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;replace "DATE" by the name of your date variable (again, upper case)&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;insert more quoted upper-case variable names into the list ('DATE' 'RF') if the date variable and RF are not the only variables to be excluded from the transformation.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Sun, 03 Mar 2019 10:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539880#M7141</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-03-03T10:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple new variables from multiple existing variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539937#M7150</link>
      <description>&lt;P&gt;Thank you for your response!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Mar 2019 20:34:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539937#M7150</guid>
      <dc:creator>va003</dc:creator>
      <dc:date>2019-03-03T20:34:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating multiple new variables from multiple existing variables</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539938#M7151</link>
      <description>&lt;P&gt;Wow that's a lot going on! Thank you very much for your help!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Mar 2019 20:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-multiple-new-variables-from-multiple-existing-variables/m-p/539938#M7151</guid>
      <dc:creator>va003</dc:creator>
      <dc:date>2019-03-03T20:35:01Z</dc:date>
    </item>
  </channel>
</rss>

