<?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: Automatically create and name new columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344858#M79292</link>
    <description>&lt;P&gt;Look at using the SASHELP VCOLUMN table which will have all the variables and you can try and create your logic.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Mar 2017 03:22:19 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-03-28T03:22:19Z</dc:date>
    <item>
      <title>Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344855#M79291</link>
      <description>&lt;P&gt;The orginal dataset&amp;nbsp;has columns like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Time   A_1   A_2   B_1   B_2   C_1&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and all the columns except 'Time' have numeric values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need the codes that can automatically add new columns: in this case,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A_All  B_All  C_All&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A_All = A_1 + A_2
B_All = B_1 + B_2
C_All = C_1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that, the column names of the original dataset, instead of 'A_:' 'B_:' 'C_:', can be anything (e.g. 'Apple_:', 'Test_:' etc.; &amp;nbsp;the new columns would accordingly&amp;nbsp;have names like 'Apple_All', 'Test_All').&lt;/P&gt;&lt;P&gt;In addition, there could be any number of 'A_:' columns: e.g. 'A_1-A_7'&lt;/P&gt;&lt;P&gt;Hard coding like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A_All = sum(of A_1-A_2);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;wouldn't work therefore.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm expecting codes that achieve the following objectives:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1.  Read column names from the original dataset - have;
2.1 for columns whose names have a format like 'alphabetic characters + underscore + numbers': e.g. Type_3
2.2 identify how many columns there are whose names have the same alphabetic characters: e.g. 'Type'
3.  create new columns whose names are 'same alphabetic characters + underscore + "All"':   e.g. Type_All
4.  the value for the new column is therefore: e.g. sum(of Type_1-Type_&amp;amp;NumColType).&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To make the question easier, the numeric part of the column names always starts from '1', and increases by 1. e.g. for 'Type_:' columns, it would be Type_1 Type_2 Type_3.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 04:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344855#M79291</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-28T04:08:42Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344858#M79292</link>
      <description>&lt;P&gt;Look at using the SASHELP VCOLUMN table which will have all the variables and you can try and create your logic.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 03:22:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344858#M79292</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-28T03:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344869#M79296</link>
      <description>Thanks for pointing out the direction.</description>
      <pubDate>Tue, 28 Mar 2017 04:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344869#M79296</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-28T04:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344921#M79316</link>
      <description>&lt;P&gt;Note that there are other types of variable lists that can be used. &amp;nbsp;Most notably:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A_total = sum(of a_: );&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A_: is an abbreviation for all variable names that begin with A_.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 14:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344921#M79316</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-28T14:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344983#M79322</link>
      <description>&lt;P&gt;Ayin,&lt;/P&gt;
&lt;P&gt;I would try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* test data */
data have;
 retain apple_1-apple_8 g3-g6 g_3-g_6 1;
run;

proc sql;
  create table columns as select name from dictionary.columns
  where libname='WORK' and memname='HAVE' and name ? '_' and type='num'
  order by upcase(name);
quit;
filename tempsas temp;
data columns;
  set columns;
  prxid=prxparse('/^(.+_)\d+\s+$/');
  if prxmatch(prxid,name);
  prefix=prxposn(prxid,1,name);
  file tempsas;
  if upcase(prefix) ne lag(upcase(prefix)) then 
    put prefix +(-1) 'All = sum(of ' prefix +(-1) ':);';
run;
 
data want;
  set have;
  %include tempsas/source2;
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;The SQL gets the names of numeric columns that contain an underscore. I sorted&amp;nbsp;them by uppercase values&amp;nbsp;in case of names like A_1, a_2 etc., which I assume that you want to group together.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step writes a temporary SAS program which does the calculation. The PRXPARSE expression looks for beginning of string, a number of any characters followed by an underscore (the paranthesis catches this with a capture buffer), followed by a number of digits and finally optional whitespace. The contents of the capture buffer is put in the PREFIX variable, when the uppercase value of that changes, a line is written to the temporary SAS file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final step includes that file to calculate the sums.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Søren&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 19:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344983#M79322</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-03-28T19:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344989#M79326</link>
      <description>&lt;P&gt;Your rules or process are missing at least one step: If your existing variable has a name with 29 or more characters you cannot create a new SAS variable by adding&amp;nbsp; "_All" to the existing name as the length would exceed the maximumn number of characters allowed.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 14:22:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/344989#M79326</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-28T14:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345059#M79347</link>
      <description>&lt;P&gt;Thank you for your comment.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In principle you may be right. But it did not seem like that was a problem, and the OP did not specify what to do in that case. This is not a general macro for system-wide use, it is an efficient solution to a user's actual problem as stated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BTW, do you know why my SAS code section is collapsed to one line when I view it? I pasted it from a SAS Studio program editor window, is it something with CRLF vs. LF as line breaks (meaning that I should edit the code in UltraEdit or similar first, or similar), or is it my browser (Opera) that has gone crazy? What does it look like in your browser?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 16:25:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345059#M79347</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-03-28T16:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345070#M79349</link>
      <description>&lt;P&gt;It collapsed. I'm not sure why., I tried to fix it but it looks fine when previewed in editor.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 16:45:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345070#M79349</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-28T16:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345108#M79357</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your comment.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In principle you may be right. But it did not seem like that was a problem, and the OP did not specify what to do in that case. This is not a general macro for system-wide use, it is an efficient solution to a user's actual problem as stated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;I assume you are responding to my comment about the variable name length. I was not responding to your post but the original poster. The OP may not realize this type of potential problem with the actual data involved.&lt;/P&gt;
&lt;P&gt;This forum has some very odd display properties that it is not always obvious who the response is from unless you use the quote function so I can see why you may think I was commenting on your code..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The bit about how your code appears may come from using the "run" icon for posting. Code seems to work better with {i} code box as it does not attempt to maintain and appearance propertlies (font color for instance) and sometimes seems the other box does not interpret the tags correctly.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 18:19:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345108#M79357</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-28T18:19:31Z</dc:date>
    </item>
    <item>
      <title>Re: Automatically create and name new columns (Advanced)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345127#M79364</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;That's intersting, I've always used the running man/notebook icon because that's what I find works best.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;I paste it into a text editor and then paste it back into the code block and that seems to fix the single line display issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 19:18:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Automatically-create-and-name-new-columns-Advanced/m-p/345127#M79364</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-28T19:18:55Z</dc:date>
    </item>
  </channel>
</rss>

