<?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 A macro to transpose all datasets from a given library in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312640#M67828</link>
    <description>&lt;P&gt;Dear all&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a number of SAS datasets stored in a library. I want to transpose each file in the same way. I use the following macro code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic;
%macro libloop;

/* List of variables to loop through*/
   proc sql noprint;
      select distinct memname
         into :dslist separated by ' '
         from sashelp.vmember
         where memtype='DATA' and libname='BS'
         order by memname;
   quit;

/*A loop to transpose all datasets*/

%let i=1;
%do %while(%scan(&amp;amp;dslist,&amp;amp;i) ne );

		proc sql; /* HERE IS PROBABLY A PROBLEM*/
			select distinct name into :vars separated by ' '
			from dictionary.columns
			where libname='BIBL' and memname='%scan(&amp;amp;dslist,&amp;amp;i)' &lt;BR /&gt;and label not in (&amp;#26;'label of var 1', 'label of var 2');
		quit;


		proc sort data=bs.%scan(&amp;amp;dslist,&amp;amp;i);
				by var1 var2;
		run;
		proc transpose data=bs.%scan(&amp;amp;dslist,&amp;amp;i)  out=bs.t_%scan(&amp;amp;dslist,&amp;amp;i);
			by var1 var2;
			var &amp;amp;vars; /*SAS does not recognize the variable &amp;amp;vars*/
		run;

	
%let i=%eval(&amp;amp;i+1); 

%end;


%mend libloop;

%libloop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is that the macro does not recognize the macro variable &amp;amp;vars that I create with the second proc sql (I get an error "ERROR: Variable VARS not found"). How to do it right?&lt;/P&gt;</description>
    <pubDate>Fri, 18 Nov 2016 15:38:37 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2016-11-18T15:38:37Z</dc:date>
    <item>
      <title>A macro to transpose all datasets from a given library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312640#M67828</link>
      <description>&lt;P&gt;Dear all&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a number of SAS datasets stored in a library. I want to transpose each file in the same way. I use the following macro code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint mlogic;
%macro libloop;

/* List of variables to loop through*/
   proc sql noprint;
      select distinct memname
         into :dslist separated by ' '
         from sashelp.vmember
         where memtype='DATA' and libname='BS'
         order by memname;
   quit;

/*A loop to transpose all datasets*/

%let i=1;
%do %while(%scan(&amp;amp;dslist,&amp;amp;i) ne );

		proc sql; /* HERE IS PROBABLY A PROBLEM*/
			select distinct name into :vars separated by ' '
			from dictionary.columns
			where libname='BIBL' and memname='%scan(&amp;amp;dslist,&amp;amp;i)' &lt;BR /&gt;and label not in (&amp;#26;'label of var 1', 'label of var 2');
		quit;


		proc sort data=bs.%scan(&amp;amp;dslist,&amp;amp;i);
				by var1 var2;
		run;
		proc transpose data=bs.%scan(&amp;amp;dslist,&amp;amp;i)  out=bs.t_%scan(&amp;amp;dslist,&amp;amp;i);
			by var1 var2;
			var &amp;amp;vars; /*SAS does not recognize the variable &amp;amp;vars*/
		run;

	
%let i=%eval(&amp;amp;i+1); 

%end;


%mend libloop;

%libloop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem is that the macro does not recognize the macro variable &amp;amp;vars that I create with the second proc sql (I get an error "ERROR: Variable VARS not found"). How to do it right?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 15:38:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312640#M67828</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-11-18T15:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: A macro to transpose all datasets from a given library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312645#M67829</link>
      <description>&lt;P&gt;The reason it doesn't recognise the list, is due to the nature of macro. &amp;nbsp;When a program is submitted, it is sent to the macro pre-processor. &amp;nbsp;This tool expands all the macro parts of the code into full Base SAS code. &amp;nbsp;That full SAS code is then sent to the compiler for execution. &amp;nbsp;Now in your code, you have a procedure proc sql which is in turn generating macro, but at the time the pre-processor works, this isn't available. &amp;nbsp;You end up in a nasty back and forth scenario. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Firstly I would, as always question why you want to transpose loads of datasets, rarely other than for review output is three any benefit to using a transposed dataset. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly if you have lots of data that require exactly the same process, the question has to be asked why the data is all split up. &amp;nbsp;Again there is rarely, if ever, any benefit to splitting data that is the same up into lots of different blocks. &amp;nbsp;All that code you have posted below is cause purely by splitting the data up. &amp;nbsp;If it was not split up, then a simple 1 step proc tranpose would be effective.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, I would suggest to rethink your process. &amp;nbsp;Put all of the data - which is all the same - into one dataset, you can keep a dataset name if you need to, I know in finance they are chained to the notion of dates in filenames for instance. &amp;nbsp;Something like:&lt;/P&gt;
&lt;PRE&gt;data all;
  set work.file_: indsname=tmp;
  length myfname $200;
  myfname=tmp;
run;

proc sort data=all;
  by ...;
run;

proc transpose data=all;
  ...
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Nov 2016 15:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312645#M67829</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-11-18T15:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: A macro to transpose all datasets from a given library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312656#M67832</link>
      <description>&lt;P&gt;1) If you are going to code macros then you need to learn to use Options MPRINT and SYMBOLGEN to examine code generated by your macro&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) You also need to pay very close attention to choice of quote marks in code:&lt;/P&gt;
&lt;P&gt;memname&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'%scan(&amp;amp;dslist,&amp;amp;i)'&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;Use of single quotes means that the macro functions and variables inside the quotes to NOT resolve.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;Your log should have shown you a number of messages about 0 records selected from dictionary.columns.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;Try&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;memname&lt;SPAN class="token operator"&gt;= "&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;%scan(&amp;amp;dslist,&amp;amp;i)"&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;&lt;SPAN class="token string"&gt;But I think that creating a separate macro variable&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let Dname = %scan(&amp;amp;dslist,&amp;amp;i);&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;&lt;SPAN class="token string"&gt;before the second proc sql call.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token string"&gt;&lt;SPAN class="token string"&gt;and using &amp;amp;Dname instead of repeated calls to %scan(&amp;amp;dslist,&amp;amp;i) might be a more robust choice in the future since you are using the same call 4 different places.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312656#M67832</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-11-18T16:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: A macro to transpose all datasets from a given library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312657#M67833</link>
      <description>&lt;P&gt;Why do I need to transpose? To make long story short - because I have external data in a wrong format. To be more precise: I have a data that are in the "wide" format, eg.:&lt;/P&gt;
&lt;P&gt;data wide;&lt;BR /&gt; input id sales2010 sales2011 no_cust2010 no_cust2011;&lt;BR /&gt; datalines;&lt;BR /&gt; 1 100 110 10 15&lt;BR /&gt; 2 200 210 20 25 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;What I need is a data in the "long" format (panel dataset):&lt;/P&gt;
&lt;P&gt;data long;&lt;BR /&gt; input id year sales no_cust;&lt;BR /&gt; datalines;&lt;BR /&gt; 1 2010 100 10&lt;BR /&gt; 1 2011 110 15&lt;BR /&gt; 2 2010 200 20&lt;BR /&gt; 2 2011 210 25 &lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do this I need to transpose the original dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original datasest are quite big (around 20 thousand records an around 1 thousand variables each), so&amp;nbsp;don't even think about combining them&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But apart from my motivation - why is creating a macro variable inside a macro via proc sql does not work in my code?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312657#M67833</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-11-18T16:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: A macro to transpose all datasets from a given library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312662#M67836</link>
      <description>&lt;P&gt;Thank you. After changing quotation marks from ' to " everything works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I missed the option symbolgen, thanks for pointing this out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2016 16:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/A-macro-to-transpose-all-datasets-from-a-given-library/m-p/312662#M67836</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-11-18T16:16:07Z</dc:date>
    </item>
  </channel>
</rss>

