<?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 Juletip #18 - Proc SQL and Macro variables. in SAS Community Nordic</title>
    <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-18-Proc-SQL-and-Macro-variables/m-p/706892#M341</link>
    <description>&lt;TABLE width="985"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="406"&gt;
&lt;P&gt;I like using Proc SQL in my data handling, and using the flexibility by expanding the code by using SAS macro functionality.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example I would like to have some values from a dataset into different macro variables. The issue is, that I don’t know the number of values=macro variables. Therefor I have 3 steps:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First I find the number of variables&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Left adjust the macro variable&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I put the values into the according number of macro variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;** Step 1 - find the number of variables **;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt; noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; select count(*) into :nobs&lt;/P&gt;
&lt;P&gt;&amp;nbsp; from sashelp.class&lt;/P&gt;
&lt;P&gt;&amp;nbsp; where age=&lt;STRONG&gt;14&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;*** Step 2 - To use the macro variable it have to be left adjusted ***;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;%let nobs=%left(&amp;amp;nobs.);&lt;/P&gt;
&lt;P&gt;%put nobs=&amp;amp;nobs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;*** Step 3 - Values are put into the correct number of macro variables ***;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt; noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; select name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; into :name1 - :name&amp;amp;nobs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from sashelp.class&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where age=&lt;STRONG&gt;14&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*** Just to see the content - different versions ***;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name1.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name2.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name3.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name4.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name1-&amp;amp;&amp;amp;name&amp;amp;nobs;&lt;/P&gt;
&lt;P&gt;%put _user_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can recommend the book: PROC SQL – Beyond the Basics Using SAS” if you want to learn more about Proc SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Merry Christmas&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 18 Dec 2020 07:00:00 GMT</pubDate>
    <dc:creator>FransHolm</dc:creator>
    <dc:date>2020-12-18T07:00:00Z</dc:date>
    <item>
      <title>Juletip #18 - Proc SQL and Macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-18-Proc-SQL-and-Macro-variables/m-p/706892#M341</link>
      <description>&lt;TABLE width="985"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="406"&gt;
&lt;P&gt;I like using Proc SQL in my data handling, and using the flexibility by expanding the code by using SAS macro functionality.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this example I would like to have some values from a dataset into different macro variables. The issue is, that I don’t know the number of values=macro variables. Therefor I have 3 steps:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First I find the number of variables&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Left adjust the macro variable&lt;/P&gt;
&lt;P&gt;3.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; I put the values into the according number of macro variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;** Step 1 - find the number of variables **;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt; noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; select count(*) into :nobs&lt;/P&gt;
&lt;P&gt;&amp;nbsp; from sashelp.class&lt;/P&gt;
&lt;P&gt;&amp;nbsp; where age=&lt;STRONG&gt;14&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;*** Step 2 - To use the macro variable it have to be left adjusted ***;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;%let nobs=%left(&amp;amp;nobs.);&lt;/P&gt;
&lt;P&gt;%put nobs=&amp;amp;nobs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;*** Step 3 - Values are put into the correct number of macro variables ***;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sql&lt;/STRONG&gt; noprint;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; select name&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; into :name1 - :name&amp;amp;nobs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from sashelp.class&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where age=&lt;STRONG&gt;14&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*** Just to see the content - different versions ***;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name1.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name2.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name3.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name4.;&lt;/P&gt;
&lt;P&gt;%put Name = &amp;amp;name1-&amp;amp;&amp;amp;name&amp;amp;nobs;&lt;/P&gt;
&lt;P&gt;%put _user_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can recommend the book: PROC SQL – Beyond the Basics Using SAS” if you want to learn more about Proc SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Merry Christmas&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 07:00:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-18-Proc-SQL-and-Macro-variables/m-p/706892#M341</guid>
      <dc:creator>FransHolm</dc:creator>
      <dc:date>2020-12-18T07:00:00Z</dc:date>
    </item>
    <item>
      <title>Re: Juletip #18 - Proc SQL and Macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-18-Proc-SQL-and-Macro-variables/m-p/706902#M342</link>
      <description>&lt;P&gt;Thank you for a nice juletip. I recently learned that the SQL Procedure lets us omit step 1 by leaving the part after the dash blank. Proc SQL creates as many macro variables as values being processed and we can capture that number in the &amp;amp;sqlobs. macro variable.&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 :name1 - 
   from sashelp.class
   where age=14;
quit;

%put Name = &amp;amp;name1.;
%put Name = &amp;amp;name2.;
%put Name = &amp;amp;name3.;
%put Name = &amp;amp;name4.;
%put Name = &amp;amp;name1-&amp;amp;&amp;amp;name&amp;amp;sqlobs.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Goes to show just how flexible the SQL Procedure is. I learned this trick from the book&amp;nbsp;&lt;A href="https://www.sas.com/store/books/categories/usage-and-reference/carpenter-s-complete-guide-to-the-sas-macro-language-third-edition/prodBK_67815_en.html" target="_self"&gt;Carpenter's Complete Guide to the SAS® Macro Language&lt;/A&gt;&amp;nbsp;by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13683"&gt;@ArtC&lt;/a&gt;. Also highly recommendable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Merry Christmas out there &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Dec 2020 08:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-18-Proc-SQL-and-Macro-variables/m-p/706902#M342</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-12-18T08:47:59Z</dc:date>
    </item>
  </channel>
</rss>

