<?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: Add a prefix/postfix to all the column names in a table when using &amp;quot;firstobs&amp;quot; for crea in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635643#M188758</link>
    <description>&lt;P&gt;Since I am new to SAS I don't have much knowledge on how to use macros in SAS, so is there any alternative approach where we can do the same without using macros.&lt;/P&gt;</description>
    <pubDate>Sun, 29 Mar 2020 14:52:56 GMT</pubDate>
    <dc:creator>Saurabh_Rana</dc:creator>
    <dc:date>2020-03-29T14:52:56Z</dc:date>
    <item>
      <title>Add a prefix/postfix to all the column names in a table when using "firstobs" for creating lag.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635592#M188739</link>
      <description>&lt;P&gt;I am using firstobs=1,2,3...etc for creating lag in the table,but what I want is that when I create a new table with lag=1 for example then all the columns name in the table should have a postfix "_1".&lt;/P&gt;&lt;P&gt;Like that I want to do "_2" for table with lag=2(firstobs=2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example =&amp;gt;&lt;/P&gt;&lt;P&gt;Column names of table "T"&lt;/P&gt;&lt;P&gt;Customer_Name &amp;nbsp;Customer_ID&lt;/P&gt;&lt;P&gt;Column names of table "T1" with lag=1(firstobs=1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;Customer_Name_1 &amp;nbsp;Customer_ID_1&lt;/P&gt;</description>
      <pubDate>Sun, 29 Mar 2020 08:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635592#M188739</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-29T08:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Add a prefix/postfix to all the column names in a table when using "firstobs" for crea</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635633#M188755</link>
      <description>&lt;P&gt;This can be done with a simple macro, once you have the names of the variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro setlag(ds,vars,lag);
%local i w;
  &amp;amp;ds(keep=&amp;amp;vars rename=(
    %do i=1 %to %sysfunc(countw(&amp;amp;vars));
       %let w=%scan(&amp;amp;vars,&amp;amp;i);
       %do; &amp;amp;w=&amp;amp;w._&amp;amp;lag%end;  /* the %do...%end here is just to avoid spurious blanks */
       %end;
     ) firstobs=&amp;amp;lag)
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The macro generates the dataset name with the options you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To get the variable names, you can use SQL:&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 into :vars separated by ' ' 
  from dictionary.columns where libname='WORK' and memname='HAVE';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(change the condition to match your input table)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then use the macro 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 in=in0;
  set %setlag(have,&amp;amp;vars,1) in=in1;
  set %setlag(have,&amp;amp;vars,2) in=in2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I put the keep= statement in the macro in case you only want some of the variables, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;set %setlag(sashelp.class,name age,3);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Mar 2020 13:28:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635633#M188755</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-03-29T13:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add a prefix/postfix to all the column names in a table when using "firstobs" for crea</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635636#M188756</link>
      <description>&lt;P&gt;Since you need to repeat &lt;EM&gt;code&lt;/EM&gt;, a macro is the tool of choice:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro create_lags(inlib=,outlib=WORK,inds=,outds=,num=1);

%local i j;

proc sql noprint;
select name into :names separated by " " from dictionary.columns
where libname = upcase("&amp;amp;inlib.") and memname = upcase("&amp;amp;inds.");
quit;

data &amp;amp;outlib..&amp;amp;outds.;
merge
  &amp;amp;inlib..&amp;amp;inds.
%do i = 1 %to &amp;amp;num.;
  &amp;amp;inlib..&amp;amp;inds. (firstobs=%eval(&amp;amp;i.+1) rename=(
  %do j = 1 %to %sysfunc(countw(&amp;amp;names));
    %scan(&amp;amp;names.,&amp;amp;j.)=_&amp;amp;i._%scan(&amp;amp;names.,&amp;amp;j.)
  %end;
  ))
%end;
;
run;

%mend;

%create_lags(inlib=sashelp,inds=class,outds=class,num=2)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Mar 2020 14:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635636#M188756</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-29T14:22:32Z</dc:date>
    </item>
    <item>
      <title>Re: Add a prefix/postfix to all the column names in a table when using "firstobs" for crea</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635643#M188758</link>
      <description>&lt;P&gt;Since I am new to SAS I don't have much knowledge on how to use macros in SAS, so is there any alternative approach where we can do the same without using macros.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Mar 2020 14:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635643#M188758</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-29T14:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add a prefix/postfix to all the column names in a table when using "firstobs" for crea</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635646#M188759</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Since I am new to SAS I don't have much knowledge on how to use macros in SAS, so is there any alternative approach where we can do the same without using macros.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you have to write the multiple lines for the "look-ahead" (because that's what you actually do) in the MERGE statement yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;merge
  have
  have (firstobs=2 rename=(var1=_1_var1))
  /* and so on */
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Mar 2020 15:02:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635646#M188759</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-29T15:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Add a prefix/postfix to all the column names in a table when using "firstobs" for crea</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635649#M188761</link>
      <description>&lt;P&gt;OK , thanks for answering&lt;/P&gt;</description>
      <pubDate>Sun, 29 Mar 2020 15:07:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-prefix-postfix-to-all-the-column-names-in-a-table-when/m-p/635649#M188761</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2020-03-29T15:07:55Z</dc:date>
    </item>
  </channel>
</rss>

