<?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 changing variable names within %do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432027#M106928</link>
    <description>&lt;P&gt;to me it doesn't seem like a macro problem in the first place. You need a nice transpose "sas code" to achieve what you want.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Jan 2018 01:42:20 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-01-30T01:42:20Z</dc:date>
    <item>
      <title>Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432026#M106927</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following "oneapp.lease" dataset:&lt;/P&gt;&lt;PRE&gt;property_id lease_id rent_amount1 rent_amount2
1           1        100          200&lt;BR /&gt;1           2        110          150&lt;BR /&gt;1           3        120          300&lt;BR /&gt;2           4        105          .&lt;/PRE&gt;&lt;P&gt;And I would like to convert it to the following:&lt;/P&gt;&lt;PRE&gt;property_id lease_id1 lease1_rent1 lease1_rent2 lease_id2 lease2_rent1 lease2_rent2 lease_id3 lease_rent1 lease_rent2&lt;BR /&gt;1           1         100          200             2          110         150          3      120          300&lt;BR /&gt;2           4         105          .             .         .            .            .         .           .  &lt;/PRE&gt;&lt;P&gt;In short, I'm trying to convert from narrow to wide, with one observation per property_id, creating variables lease1_rent1 to leaseN_rentM&lt;/P&gt;&lt;P&gt;where there is a predefined max N and M (e.g. N= 10 and M = 3)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code currently looks as follows:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let i = 1;
%macro create_leases;
DATA lease_wide;
set oneapp.lease;
by property_id;
if first.property_id then
&amp;amp;i. = 1;
%do j = 1 %to &amp;amp;max_rent_changes.;
lease&amp;amp;i._rent_amount&amp;amp;j. = rent_amount&amp;amp;j.;
%end;
%let i = &amp;amp;i. + 1;
if last.property_id then
output;
RUN;
%mend;&lt;BR /&gt;%create_leases()&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm quite new to macros, and not sure why the above doesn't work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 01:38:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432026#M106927</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2018-01-30T01:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432027#M106928</link>
      <description>&lt;P&gt;to me it doesn't seem like a macro problem in the first place. You need a nice transpose "sas code" to achieve what you want.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 01:42:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432027#M106928</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-30T01:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432031#M106929</link>
      <description>&lt;P&gt;The first thing to learn about macros is that they don't process data.&amp;nbsp; Instead, they construct a program.&amp;nbsp; When the constructed program runs, that processes the data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To give you an example from your non-working program, consider this combination:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let i=1;&lt;/P&gt;
&lt;P&gt;if first.property_id then &amp;amp;i.=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All macro language does is make the text substitution, to create a valid (or possibly invalid in this case) SAS statement.&amp;nbsp; That means your two statements produce:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if first.property_id then 1=1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Clearly, this is not the SAS statement that you intended.&amp;nbsp; So the basic idea when learning macros is that you first construct a working program that does not use macros at all.&amp;nbsp; Then you consider how macro language could be used to generate that program.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 01:50:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432031#M106929</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-30T01:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432039#M106932</link>
      <description>&lt;P&gt;Here is a demo of an approach you actually can think of and/or alternatively try &lt;STRONG&gt;arrays&lt;/STRONG&gt;. I'm too lazy now but I am sure you can perfect the code in the demo with little modification&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input property_id lease_id rent_amount1 rent_amount2 ;
datalines;
1           1        100          200
1           2        110          150
1           3        120          300
2           4        105          .
;

proc transpose data=have out=_have;
by property_id;
var lease_id rent_amount1 rent_amount2;
run;


proc transpose data=_have out=_have1;
by property_id _name_;
run;

data temp;
set _have1;
by 	property_id _name_;
if first._name_ then n=0;
n+1;
run;

proc sort data=temp out=_t;
by 	property_id n;
run;

proc transpose data=_t(keep=property_id col1) out=want;
by property_id ;
var col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jan 2018 02:25:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432039#M106932</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-30T02:25:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432169#M106997</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input property_id lease_id rent_amount1 rent_amount2 ;
datalines;
1           1        100          200
1           2        110          150
1           3        120          300
2           4        105          .
;

proc sql noprint;
select max(n) into : n
 from (select count(*) as n from have group by property_id);
quit;
proc summary data=have;
by property_id;
output out=want idgroup(out[&amp;amp;n] (lease_id rent_amount1 rent_amount2 )=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Jan 2018 13:01:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432169#M106997</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-30T13:01:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432277#M107051</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the reply, this works. Could I ask what is happening in the&amp;nbsp;PROC SUMMARY datastep? How&amp;nbsp;exactly does the [&amp;amp;n] work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2018 15:54:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432277#M107051</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2018-01-30T15:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432696#M107188</link>
      <description>&lt;P&gt;&amp;amp;n is the max number of each group .&lt;/P&gt;
&lt;P&gt;SAS Documentation is the best teacher .Check PROC SUMMARY/MEANS 's doc .&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 12:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432696#M107188</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-31T12:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432772#M107222</link>
      <description>&lt;P&gt;While proc summary can definitely be used to make a long file wide, it doesn't end up with the variable names you wanted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A group of us wrote a macro a couple of years ago that, I think, does exactly what you are trying to accomplish. You can download the macro from:&amp;nbsp;&lt;A href="http://www.sascommunity.org/wiki/A_Better_Way_to_Flip_(Transpose)_a_SAS_Dataset" target="_blank"&gt;http://www.sascommunity.org/wiki/A_Better_Way_to_Flip_(Transpose)_a_SAS_Dataset&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you download the macro and run it, the following call will accomplish the task:&lt;/P&gt;
&lt;PRE&gt;%transpose(data=have, out=want, by=property_id, id=lease_id, var_first=no,
 prefix=lease, delimiter=_, var=rent_amount1 rent_amount2)
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 14:56:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432772#M107222</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-01-31T14:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432777#M107225</link>
      <description>&lt;P&gt;Please Try &lt;STRONG&gt;Proc Transpose&lt;/STRONG&gt;, it is much simpler than using a macro to do this,&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432777#M107225</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-01-31T15:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432782#M107227</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138619"&gt;@Satish_Parida&lt;/a&gt;: Please explain your comment. The macro I proposed requires less typing, gives more control than proc transpose, and runs 50 to 100 times faster than proc transpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432782#M107227</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-01-31T15:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating changing variable names within %do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432803#M107234</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp; The replay was over the macro created by the User.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;To successfully execute the macro: user specified, as we know the maximum number of records are their datatypes, that can be achieved by using SAS arrays and lots of retain variables.&lt;/P&gt;&lt;P&gt;While A simple transpose over variable&amp;nbsp;property_id will transpose the whole table to users prospective structure how ever, the column names wont be ideal to read.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for sharing the article.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 15:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-changing-variable-names-within-do-loop/m-p/432803#M107234</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2018-01-31T15:53:50Z</dc:date>
    </item>
  </channel>
</rss>

