<?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: Help with a macro that removes a certain prefix from all variables in a table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900690#M355957</link>
    <description>&lt;P&gt;Your data set name must be in CAPITAL letters&lt;/P&gt;</description>
    <pubDate>Mon, 30 Oct 2023 14:30:46 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-10-30T14:30:46Z</dc:date>
    <item>
      <title>Help with a macro that removes a certain prefix from all variable names in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900676#M355947</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;I am in base SAS, and I would like to replace the prefix from the variable name of all variables in a dataset. For example, I have a dataset with 125 columns that all begin with "SGBSTDN_", and I want to remove the "SGBSTDN_" from the variable name for each of the 125 columns. I have written the following macro code to accomplish this, but when I try to call the macro, I get an error (screenshot below). Here is my code:&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;P&gt;%macro rmv_pfx(prefix, dataset);&lt;BR /&gt;/* Get the list of variables in the dataset */&lt;BR /&gt;%let dsid = %sysfunc(open(&amp;amp;dataset));&lt;BR /&gt;%let num_vars = %sysfunc(attrn(&amp;amp;dsid, nvars));&lt;BR /&gt;%do i = 1 %to &amp;amp;num_vars;&lt;BR /&gt;%let var_name = %sysfunc(varname(&amp;amp;dsid, &amp;amp;i));&lt;BR /&gt;%let new_var_name = %scan(&amp;amp;var_name, -1, &amp;amp;prefix);&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;/* Rename the variable */&lt;BR /&gt;rename &amp;amp;var_name = &amp;amp;new_var_name;&lt;BR /&gt;%end;&lt;BR /&gt;%let dsid = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And, here is what I am typing to call the macro. The name of my dataset is "d".&lt;BR /&gt;%rmv_pfx(SGBSTDN_, d);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help let me know why SAS is throwing the error in the screenshot?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="screenshot.JPG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89241i4F9C9E401BEF5E5B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="screenshot.JPG" alt="screenshot.JPG" /&gt;&lt;/span&gt;&lt;P&gt; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 30 Oct 2023 13:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900676#M355947</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T13:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900678#M355948</link>
      <description>&lt;P&gt;No macros needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */
proc sql noprint;
    select cats(name,'=',substr(name,9)) into :renames separated by ' ' from dictionary.columns where 
    upcase(name) eqt 'SGBSTDN_' and memname='YOURDATASETNAME' and library='WORK';
quit;

proc datasets library=work nolist;
    modify yourdatasetname;
    rename &amp;amp;renames;
run; quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe your code fails because the RENAME statement must be in a SAS data step or PROC DATASETS. In the future, do NOT show us tiny portions of the LOG, we needed to see the ENTIRE log (not just the errors).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 13:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900678#M355948</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-30T13:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900684#M355952</link>
      <description>&lt;P&gt;Thank you for this. I believe I typed your code exactly (except I replaced "your dataset" with the name of my dataset). Unfortunately I got an error, which I'll paste the screenshot below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't copy the text (either into or out of SAS) since I'm working in SAS on a secured desktop environment without internet, but I can show a screenshot. This is the log with the error codes:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="screenshot.JPG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89242i1F867723E89BCA4F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="screenshot.JPG" alt="screenshot.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; Any ideas? Many thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900684#M355952</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T14:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900685#M355953</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s solution was untested, so this was expected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use LIBNAME = "WORK" instead. Always use caps when you use dictionary.tables/columns.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:24:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900685#M355953</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2023-10-30T14:24:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900686#M355954</link>
      <description>&lt;P&gt;Sorry about that. I made a mistake, it should be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    upcase(name) eqt 'SGBSTDN_' and memname='YOURDATASETNAME' and libname='WORK';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and yes, the data set name and libname must be in capital letters&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900686#M355954</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-30T14:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900687#M355955</link>
      <description>I would recommend marking &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;'s response as the correct one.</description>
      <pubDate>Mon, 30 Oct 2023 14:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900687#M355955</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2023-10-30T14:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900689#M355956</link>
      <description>&lt;P&gt;Thank you, that did resolve the first error. The only thing that seems to be sticking is it does not seem to like the line&lt;/P&gt;&lt;PRE&gt;rename &amp;amp;renames;&lt;/PRE&gt;&lt;P&gt;I have pasted the new log below. Thanks again.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="screenshot.JPG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89243i440DD68C8BA1A183/image-size/medium?v=v2&amp;amp;px=400" role="button" title="screenshot.JPG" alt="screenshot.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900689#M355956</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T14:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900690#M355957</link>
      <description>&lt;P&gt;Your data set name must be in CAPITAL letters&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:30:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900690#M355957</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-30T14:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900691#M355958</link>
      <description>&lt;P&gt;Success! Thanks a ton!&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900691#M355958</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T14:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900692#M355959</link>
      <description>&lt;P&gt;In my haste I accidentally accepted the one where you corrected libname, instead of&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;I don't suppose there's a way to undo this?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:35:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900692#M355959</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T14:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900693#M355960</link>
      <description>&lt;P&gt;I believe that if you scroll to the message you marked correct, you can un-mark it as correct via a button that appears there.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900693#M355960</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-30T14:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900695#M355961</link>
      <description>&lt;P&gt;Yes, I did at least think of that &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; but there is no button to undo it. Here is how it appears.&amp;nbsp; Sorry about that!!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sarah_Caro_0-1698676634026.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89244iBEA75F0C94B10071/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sarah_Caro_0-1698676634026.png" alt="Sarah_Caro_0-1698676634026.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 14:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900695#M355961</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T14:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900704#M355965</link>
      <description>&lt;P&gt;I think if you click the 'hamburger' button (three lines) under the photo, there may be an option in that menu to unmark as correct.&amp;nbsp; But it may not be there, depending on your rank, and amount of time since you marked it correct.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 15:09:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900704#M355965</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-10-30T15:09:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with a macro that removes a certain prefix from all variables in a table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900706#M355966</link>
      <description>&lt;P&gt;Great, thanks! I was able to do it and then marked PaigeMiller's post (the later one where he corrected the code) as the solution. I hope that is the convention here - I didn't mark the first one with the full code, because it contained a slight error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again to all.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 15:12:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-a-macro-that-removes-a-certain-prefix-from-all/m-p/900706#M355966</guid>
      <dc:creator>Sarah_Caro</dc:creator>
      <dc:date>2023-10-30T15:12:25Z</dc:date>
    </item>
  </channel>
</rss>

