<?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: Create a variable using macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246156#M46030</link>
    <description>Thank you for hints. It's not a college assignment. I am learning the concepts on my own. I invested a good amount of time accomplishing this task with PROC SQL SELECT INTO macro variable. I know this kind of situation can be handled with CALL SYMPUT - call symput('nvars' || strip(_n_), name) and then use LOOP and SYMGET in data step. I wanted to know if this can be done with SELECT INTO macro variable?&lt;BR /&gt;</description>
    <pubDate>Tue, 26 Jan 2016 17:36:09 GMT</pubDate>
    <dc:creator>Ujjawal</dc:creator>
    <dc:date>2016-01-26T17:36:09Z</dc:date>
    <item>
      <title>Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246149#M46023</link>
      <description>&lt;P&gt;I am trying to create a variable in a new data set using a macro variable. I am trying to store names of numeric variable in a macro variable and then populate names in a variable in a new data set. I know it can be easily done with PROC CONTENTS. It's for my learning. I am not working on a real life project.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro test;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select name into: nvar separated by " "&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where LIBNAME = "SASHELP"&lt;BR /&gt;and MEMNAME = "CLASS"&lt;BR /&gt;and type = "num";&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let varn=%sysfunc(countw(&amp;amp;nvar%str( )));&lt;BR /&gt;%DO i=1 %TO &amp;amp;varn.;&lt;BR /&gt;%let varName = %qscan(%sysfunc(compbl(&amp;amp;nvar)),&amp;amp;i,%str( ));&lt;BR /&gt;%put &amp;amp;varName;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;var1 = symget(&amp;amp;varName.);&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;%mend;&lt;BR /&gt;%test;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in anticipation. Any help would be highly appreciated!&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:23:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246149#M46023</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-01-26T17:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246151#M46025</link>
      <description>&lt;P&gt;If it's an exercise...think about where the loop needs to actually be. If you have X macro variables does the loop need to be outside the data step or inside. And do you actually need a macro loop or can you go towards a data step or is that not what you want to 'practice'?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246151#M46025</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T17:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246156#M46030</link>
      <description>Thank you for hints. It's not a college assignment. I am learning the concepts on my own. I invested a good amount of time accomplishing this task with PROC SQL SELECT INTO macro variable. I know this kind of situation can be handled with CALL SYMPUT - call symput('nvars' || strip(_n_), name) and then use LOOP and SYMGET in data step. I wanted to know if this can be done with SELECT INTO macro variable?&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246156#M46030</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-01-26T17:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246157#M46031</link>
      <description>&lt;P&gt;Since you're doing this as an exercise and learning experience, let me give you a few general tips.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pick meaningful names that are easy to interpret.&amp;nbsp; There is no excuse for naming one macro variable NVAR and another VARN.&amp;nbsp; That combination of names are too difficult to differentiate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Names that used only within a macro should be defined with a %LOCAL statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%local i nvar varn varname;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Selecting out one name from a list can be simplified:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let varName = %qscan(&amp;amp;nvar,&amp;amp;i,%str( ));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When a blank as your delimiter, %QSCAN does not need to compress consecutive blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When SYMGET creates a variable in a SAS data set, the length of that variable will be $200.&amp;nbsp; You might want to define a shorter length first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not clear what your final objective is.&amp;nbsp; If AGE is one of the variables in SASHELP.CLASS, which of these do you want to generate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;age = .;&lt;/P&gt;
&lt;P&gt;var1 = 'age';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it's the second one, you might want to consider skipping the macro language entirely:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;create table want as select name as var1&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where LIBNAME = "SASHELP"&lt;BR /&gt;and MEMNAME = "CLASS"&lt;BR /&gt;and type = "num";&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry if this sounds harsh.&amp;nbsp; It's not intended to be ... just a stream of my reactions to the programming techniques.&amp;nbsp; If you clarify the final objective, we can look at tailoring the second half of the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246157#M46031</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-26T17:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246158#M46032</link>
      <description>&lt;P&gt;Select INTO is how you create your variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm referring to the loop where you're trying to assign the values to a variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It doesn't make sense to have that loop outside the data step since you're assigning variabes. Your %do loop should be in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not assign the macro variables to a variable and parse it out with SCAN?&lt;/P&gt;
&lt;P&gt;Or assign it to a temporary array and then use the array loop to output.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can use a %do loop within the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think trying to assign the values looping a data step over and over is inefficient and not worth learning.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246158#M46032</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T17:39:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246159#M46033</link>
      <description>&lt;P&gt;I think the point also is, if you're truly trying to learn, having someone provide the solution isn't going to help as much as if you find the answer yourself.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246159#M46033</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T17:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246184#M46043</link>
      <description>&lt;P&gt;Thank for your reply. I have corrected my earlier code. However, loop seems to be not working right. It is returning same value in all the 3 observations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro test;&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select name into: nvar separated by " "&lt;BR /&gt;from dictionary.columns&lt;BR /&gt;where LIBNAME = "SASHELP"&lt;BR /&gt;and MEMNAME = "CLASS"&lt;BR /&gt;and type = "num";&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;%let cntvar = %sysfunc(countw(&amp;amp;nvar));&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;%do i=1 %TO &amp;amp;cntvar.;&lt;BR /&gt;%let varName =%scan(&amp;amp;nvar,&amp;amp;i);&lt;BR /&gt;%put &amp;amp;varName;&lt;BR /&gt;var1 = symget('varName');&lt;BR /&gt;output;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;/P&gt;
&lt;P&gt;%test;&lt;BR /&gt;&lt;BR /&gt;The output should look like below (Only 1 variable and 3 observations) -&lt;/P&gt;
&lt;P&gt;var1&lt;BR /&gt;Age&lt;BR /&gt;Height&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Weight&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246184#M46043</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-01-26T18:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246196#M46046</link>
      <description>&lt;P&gt;Getting much closer to a solution, though.&amp;nbsp; Picture this DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;var1 = symget('varName');&lt;BR /&gt;output;&lt;BR /&gt;var1 = symget('varName');&lt;BR /&gt;output;&lt;BR /&gt;var1 = symget('varName');&lt;BR /&gt;output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's actually what your DATA step looks like.&amp;nbsp; Macro language isn't executing its statements in the middle of your DATA step.&amp;nbsp; Rather, it is helping generate the statements that become part of your DATA step.&amp;nbsp; Here's one way that you could change the outcome:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;length var1 $ 32;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%do i=1 %TO &amp;amp;cntvar.;&lt;BR /&gt;%let varName =%scan(&amp;amp;nvar,&amp;amp;i);&lt;BR /&gt;&amp;nbsp; var1 = "&amp;amp;varName";&lt;BR /&gt;&amp;nbsp; output;&lt;BR /&gt;%end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now &amp;amp;VARNAME will change each time through the loop, and the resulting DATA step code will change along with it.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 19:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246196#M46046</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-26T19:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create a variable using macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246223#M46049</link>
      <description>Thanks a ton!</description>
      <pubDate>Tue, 26 Jan 2016 21:03:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-a-variable-using-macro-variable/m-p/246223#M46049</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2016-01-26T21:03:16Z</dc:date>
    </item>
  </channel>
</rss>

