<?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 New Variables With Dynamic Suffix in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650021#M22326</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't recall if it is possible to do it without 26 datasets, but as a replacement maybe this will help:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input product : $ production_year quarter amount count;
cards;
A 1984 0 100 1
A 1984 1 200 2
A 1984 2 300 3
A 1985 0 400 4
A 1985 1 500 5
B 1984 0 100 1
B 1984 1 200 2
B 1984 2 300 3
B 1985 0 400 4
B 1985 1 500 5
;
run;


%macro tr(have,varlist);
  %local i var;
  %let i=1;
  %let var = %scan(&amp;amp;varlist., &amp;amp;i.);

  %do %while(&amp;amp;var. ne);
    proc transpose data = have out = want_&amp;amp;i.(drop=_name_) prefix=&amp;amp;var._ delimiter=_;
      by product;
      var &amp;amp;var.;
      id production_year quarter;
      format quarter z2.;
    run;

    %let i=%eval(&amp;amp;i.+1);
    %let var = %scan(&amp;amp;varlist., &amp;amp;i.);
  %end;
%mend tr;

%tr(have, amount count);

data want;
  merge want_:;
  by product;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Fri, 22 May 2020 21:00:22 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-05-22T21:00:22Z</dc:date>
    <item>
      <title>Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/649992#M22320</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create a new batch of all the variables in my dataset for each value of two variables: (1) production_year and (2) quarter. For instance, if production_year = 1984 and quarter = 00, then I want to create and add a new batch of variables to my dataset with the suffix "_1984_00".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the code below, I have to first subset my data by production_year and quarter and then merge them back together. I am very new to macros . . . I was able to find a macro online and modify it for static renaming purposes. Since I am working with more than 80 production_year and quarter combinations, I am trying to find a way to do this more dynamically.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro rename(oldvarlist, suffix);
	%let k=1;
	%let old = %scan(&amp;amp;oldvarlist, &amp;amp;k);
		%do %while("&amp;amp;old" NE "");
		rename &amp;amp;old = &amp;amp;old.&amp;amp;suffix;
			%let k = %eval(&amp;amp;k + 1);
		%let old = %scan(&amp;amp;oldvarlist, &amp;amp;k);
	%end;
%mend;

data ohprod.production_1984_00;
	set ohprod.combined_production;
		WHERE	PRODUCTION_YEAR = "1984" and
				QUARTER = "00";
run;
     
data ohprod.production_1984_00_final;
	set ohprod.production_1984_00;

		%rename (ACRES_NO APPROVED_BY COMMENTS_PRODUCTION DAYS DECIMAL_WORKING_INTEREST DT_FIRST_PRODUCTION
				DT_MODIFIED_PRODUCTION DT_RECEIVED ENTERED_BY FLAG_1 MAXIMUM_STORAGE_CAPACITY 
				OIL_STORAGE OWNER_NAME OWNER_NO_PRODUCTION PARCEL_NO PERMIT PRODUCTION_BRINE PRODUCTION_GAS
				PRODUCTION_OIL PRODUCTION_OIL_GAS_TOTAL PRODUCTION_YEAR QUARTER TAXING_DISTRICT UPSIZE_TS_PRODUCTION
				WELL_NAME_PRODUCTION WELL_NO_PRODUCTION YEAR_ORDER, _1984_00);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 May 2020 19:22:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/649992#M22320</guid>
      <dc:creator>JJ_83</dc:creator>
      <dc:date>2020-05-22T19:22:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650005#M22321</link>
      <description>IME its easier to transpose your data to a long format and then transpose it so that your variables are named dynamically.</description>
      <pubDate>Fri, 22 May 2020 20:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650005#M22321</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-22T20:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650007#M22322</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do you want to do something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input product : $ production_year quarter amount count;
cards;
A 1984 0 100 1
A 1984 1 200 2
A 1984 2 300 3
A 1985 0 400 4
A 1985 1 500 5
B 1984 0 100 1
B 1984 1 200 2
B 1984 2 300 3
B 1985 0 400 4
B 1985 1 500 5
;
run;

proc transpose data = have out = want1(drop=_name_) prefix=amount_ delimiter=_;
  by product;
  var amount;
  id production_year quarter;
run;

proc transpose data = have out = want2(drop=_name_) prefix=count_ delimiter=_;
  by product;
  var count;
  id production_year quarter;
run;

data want;
  merge want1 want2;
  by product;
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;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 20:15:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650007#M22322</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-22T20:15:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650013#M22324</link>
      <description>&lt;P&gt;Hi Bart,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, that is what I want!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have about 26 variables, so is there a way to do this so that I don't have to create and then combine 26 datasets? If not, that's totally fine . . . this way is already a huge improvement from the way I've been doing it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 20:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650013#M22324</guid>
      <dc:creator>JJ_83</dc:creator>
      <dc:date>2020-05-22T20:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650021#M22326</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't recall if it is possible to do it without 26 datasets, but as a replacement maybe this will help:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input product : $ production_year quarter amount count;
cards;
A 1984 0 100 1
A 1984 1 200 2
A 1984 2 300 3
A 1985 0 400 4
A 1985 1 500 5
B 1984 0 100 1
B 1984 1 200 2
B 1984 2 300 3
B 1985 0 400 4
B 1985 1 500 5
;
run;


%macro tr(have,varlist);
  %local i var;
  %let i=1;
  %let var = %scan(&amp;amp;varlist., &amp;amp;i.);

  %do %while(&amp;amp;var. ne);
    proc transpose data = have out = want_&amp;amp;i.(drop=_name_) prefix=&amp;amp;var._ delimiter=_;
      by product;
      var &amp;amp;var.;
      id production_year quarter;
      format quarter z2.;
    run;

    %let i=%eval(&amp;amp;i.+1);
    %let var = %scan(&amp;amp;varlist., &amp;amp;i.);
  %end;
%mend tr;

%tr(have, amount count);

data want;
  merge want_:;
  by product;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 21:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650021#M22326</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-22T21:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650023#M22327</link>
      <description>&lt;P&gt;If all your variables are numeric, go to a fully long data set and then transpose it back. Then when you transpose you can use multiple variables, including the variable name as part of the ID statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Variable Value Year Quarter
var1   24    2008 1
var2   25    2008 1
...

var1  38  2020 1
...
var15 34 2020 1
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 May 2020 21:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650023#M22327</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-22T21:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650026#M22328</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;- thanks for reminding me the solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&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/329582"&gt;@JJ_83&lt;/a&gt;&amp;nbsp;the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input product : $ production_year quarter amount count;
cards;
A 1984 0 100 1
A 1984 1 200 2
A 1984 2 300 3
A 1985 0 400 4
A 1985 1 500 5
B 1984 0 100 1
B 1984 1 200 2
B 1984 2 300 3
B 1985 0 400 4
B 1985 1 500 5
;
run;


data have2;
  set have;
  array X amount count;

  do over X;
    var_val = X;
    var_name=vname(X);
    output;
  end;
  drop amount count;
run;

proc transpose data = have2 out = want(drop=_name_) delimiter=_;
  by product;
  var var_val;
  id var_name production_year quarter;
  format quarter z2.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 21:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650026#M22328</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-22T21:09:26Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650058#M22332</link>
      <description>&lt;P&gt;This is an excellent justification for sas to enhance proc transpose so that it can create two (or more) output datasets (want1 and want2 in this example) in one pass, instead of the current requirement to use two passes.&amp;nbsp; Needlessly expensive for large datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't think the transpose macro offered by Art Tabachnek (&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;) et al. does this, but you might want to look at&lt;/P&gt;
&lt;P&gt;&lt;LI-MESSAGE title="A better way to FLIP (i.e, transpose/make wide) a dataset" uid="433620" url="https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-a-dataset/m-p/433620#U433620" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 May 2020 01:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650058#M22332</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-05-23T01:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650070#M22334</link>
      <description>Mark, &lt;BR /&gt;&lt;BR /&gt;100% agree that SAS should enhance proc transpose. Also thanks for the link to the article.&lt;BR /&gt;&lt;BR /&gt;Bart</description>
      <pubDate>Sat, 23 May 2020 08:03:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650070#M22334</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-23T08:03:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650088#M22335</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;: The %transpose macro would require two passes of the data as it was only designed for a single variable id. However, the first data pass would be quite simple and, other than simplicity, the macro would have the additional benefit of retaining each variables metadata. e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename tr url 'https://raw.githubusercontent.com/art297/transpose/master/transpose.sas';
%include tr ;

data need;
  set have;
  newid=catx('_',production_year,put(quarter,z2.));
run;

%transpose(data=need, out=want2, id=newid,by=product,
 Guessingrows=1000, var=amount count, var_first=yes, delimiter=_)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 May 2020 13:51:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/650088#M22335</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2020-05-23T13:51:45Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Variables With Dynamic Suffix</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/672828#M23478</link>
      <description>&lt;P&gt;Thank you, this worked great!&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 13:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Creating-New-Variables-With-Dynamic-Suffix/m-p/672828#M23478</guid>
      <dc:creator>JJ_83</dc:creator>
      <dc:date>2020-07-28T13:46:23Z</dc:date>
    </item>
  </channel>
</rss>

