<?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: call variable names in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307295#M65788</link>
    <description>&lt;P&gt;If your code is as shown, you declare your freqbynum macro AFTER you call it. It needs to exist before.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Order of operation.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Oct 2016 03:07:25 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-10-26T03:07:25Z</dc:date>
    <item>
      <title>call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307291#M65786</link>
      <description>&lt;P&gt;I want to create a loop for calling variable names, such at i-th&amp;nbsp;loop, the name of the&amp;nbsp;first i column will display, however, my code is only working for the first loop. this is my code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEMP;
 INPUT ID $ NAME $ SALARY DEPARTMENT $;
 DATALINES;
 1 Rick 623.3 IT
 2 Dan 515.2 Operations
 3 Michelle 611 IT
 4 Ryan 729 HR
 5 Gary 843.25 Finance
 6 Nina 578 IT
 7 Simon 632.8 Operations
 8 Guru 722.5 Finance
 ;
RUN;

%macro freqbynum(data=,num=); 
 %local varlist;
 proc transpose data=&amp;amp;data(obs=0) out=varnames;
 var _all_;
 run;
 PROC SQL;
 CREATE TABLE &amp;amp;data AS
 SELECT * FROM varnames(obs=&amp;amp;num);
 QUIT;
 run;
 PROC PRINT data = &amp;amp;data;
 RUN;
%mend; 
%MACRO loop;
 %DO i = 1 %to 4;
 %freqbynum(data= TEMP ,num= &amp;amp;i);
 %END;
%MEND loop;
%loop;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'd appreciate it if someone tells me what is my mistake, and how I can figure it out?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 03:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307291#M65786</guid>
      <dc:creator>Marzi</dc:creator>
      <dc:date>2016-10-26T03:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307295#M65788</link>
      <description>&lt;P&gt;If your code is as shown, you declare your freqbynum macro AFTER you call it. It needs to exist before.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Order of operation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 03:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307295#M65788</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-26T03:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307296#M65789</link>
      <description>the problem is not from the order of MACRO, I fix it, but still getting the same answer.</description>
      <pubDate>Wed, 26 Oct 2016 03:23:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307296#M65789</guid>
      <dc:creator>Marzi</dc:creator>
      <dc:date>2016-10-26T03:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307298#M65790</link>
      <description>&lt;P&gt;Why not query the sashelp.vcolumn data instead?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I dont see anything in your code. Post the full log using MPRINT and SYMBOLGEN&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 03:33:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307298#M65790</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-26T03:33:03Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307300#M65792</link>
      <description>&lt;P&gt;You overwrite the original table. That is not right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEMP;
 INPUT ID $ NAME $ SALARY DEPARTMENT $;
 DATALINES;
 1 Rick 623.3 IT
 2 Dan 515.2 Operations
 3 Michelle 611 IT
 4 Ryan 729 HR
 5 Gary 843.25 Finance
 6 Nina 578 IT
 7 Simon 632.8 Operations
 8 Guru 722.5 Finance
 ;
RUN;

%macro freqbynum(data=,num=); 
 %local varlist;
 proc transpose data=&amp;amp;data(obs=0) out=_varnames;
 var _all_;
 run;
 PROC SQL;
 CREATE TABLE _&amp;amp;data AS
 SELECT * FROM _varnames(obs=&amp;amp;num);
 QUIT;
 run;
 PROC PRINT data = _&amp;amp;data;
 RUN;
%mend; 
%MACRO loop;
 %DO i = 1 %to 4;
 %freqbynum(data= TEMP ,num= &amp;amp;i);
 %END;
%MEND loop;
%loop;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Oct 2016 03:41:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307300#M65792</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-26T03:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307302#M65793</link>
      <description>&lt;P&gt;Or Simple as IML.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEMP;
 INPUT ID $ NAME $ SALARY DEPARTMENT $;
 DATALINES;
 1 Rick 623.3 IT
 2 Dan 515.2 Operations
 3 Michelle 611 IT
 4 Ryan 729 HR
 5 Gary 843.25 Finance
 6 Nina 578 IT
 7 Simon 632.8 Operations
 8 Guru 722.5 Finance
 ;
RUN;

proc iml;
x=contents(temp);
do i=1 to nrow(x);
print (x[1:i]);
end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Oct 2016 03:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307302#M65793</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-26T03:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307303#M65794</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1365      DATALINES;

NOTE: The data set WORK.TEMP has 8 observations and 4 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


1374          ;
1375  RUN;
1376
1377  %macro freqbynum(data=,num=);
1378      %local varlist;
1379      proc transpose data=&amp;amp;data(obs=0) out=varnames;
1380          var _all_;
1381      run;
1382      PROC SQL;
1383          CREATE TABLE &amp;amp;data AS
1384          SELECT * FROM varnames(obs=&amp;amp;num);
1385      QUIT;
1386      run;
1387      PROC PRINT data = &amp;amp;data;
1388      RUN;
1389  %mend;
1390  %MACRO loop;
1391      %DO i = 1 %to 4;
1392         %freqbynum(data= TEMP ,num= &amp;amp;i);
1393      %END;
1394  %MEND loop;
1395
1396  options mprint mlogic SYMBOLGEN;
1397  %loop;
MLOGIC(LOOP):  Beginning execution.
MLOGIC(LOOP):  %DO loop beginning; index variable I; start value is 1; stop value is 4; by value is 1.
MLOGIC(FREQBYNUM):  Beginning execution.
SYMBOLGEN:  Macro variable I resolves to 1
MLOGIC(FREQBYNUM):  Parameter DATA has value TEMP
MLOGIC(FREQBYNUM):  Parameter NUM has value 1
MLOGIC(FREQBYNUM):  %LOCAL  VARLIST
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   proc transpose data=TEMP(obs=0) out=varnames;
MPRINT(FREQBYNUM):   var _all_;
MPRINT(FREQBYNUM):   run;

NOTE: Numeric variables in the input data set will be converted to character in the output data set.
NOTE: There were 0 observations read from the data set WORK.TEMP.
NOTE: The data set WORK.VARNAMES has 4 observations and 1 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


MPRINT(FREQBYNUM):   PROC SQL;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
SYMBOLGEN:  Macro variable NUM resolves to 1
MPRINT(FREQBYNUM):   CREATE TABLE TEMP AS SELECT * FROM varnames(obs=1);
NOTE: Table WORK.TEMP created, with 1 rows and 1 columns.

MPRINT(FREQBYNUM):   QUIT;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


MPRINT(FREQBYNUM):   run;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   PROC PRINT data = TEMP;
MPRINT(FREQBYNUM):   RUN;

NOTE: There were 1 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.05 seconds
      cpu time            0.01 seconds


MLOGIC(FREQBYNUM):  Ending execution.
MPRINT(LOOP):  ;
MLOGIC(LOOP):  %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(FREQBYNUM):  Beginning execution.
SYMBOLGEN:  Macro variable I resolves to 2
MLOGIC(FREQBYNUM):  Parameter DATA has value TEMP
MLOGIC(FREQBYNUM):  Parameter NUM has value 2
MLOGIC(FREQBYNUM):  %LOCAL  VARLIST
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   proc transpose data=TEMP(obs=0) out=varnames;
MPRINT(FREQBYNUM):   var _all_;
MPRINT(FREQBYNUM):   run;

NOTE: There were 0 observations read from the data set WORK.TEMP.
NOTE: The data set WORK.VARNAMES has 1 observations and 2 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


MPRINT(FREQBYNUM):   PROC SQL;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
SYMBOLGEN:  Macro variable NUM resolves to 2
MPRINT(FREQBYNUM):   CREATE TABLE TEMP AS SELECT * FROM varnames(obs=2);
NOTE: Table WORK.TEMP created, with 1 rows and 2 columns.

MPRINT(FREQBYNUM):   QUIT;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds


MPRINT(FREQBYNUM):   run;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   PROC PRINT data = TEMP;
MPRINT(FREQBYNUM):   RUN;

NOTE: There were 1 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.05 seconds
      cpu time            0.00 seconds


MLOGIC(FREQBYNUM):  Ending execution.
MPRINT(LOOP):  ;
MLOGIC(LOOP):  %DO loop index variable I is now 3; loop will iterate again.
MLOGIC(FREQBYNUM):  Beginning execution.
SYMBOLGEN:  Macro variable I resolves to 3
MLOGIC(FREQBYNUM):  Parameter DATA has value TEMP
MLOGIC(FREQBYNUM):  Parameter NUM has value 3
MLOGIC(FREQBYNUM):  %LOCAL  VARLIST
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   proc transpose data=TEMP(obs=0) out=varnames;
MPRINT(FREQBYNUM):   var _all_;
MPRINT(FREQBYNUM):   run;

NOTE: There were 0 observations read from the data set WORK.TEMP.
NOTE: The data set WORK.VARNAMES has 2 observations and 2 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


MPRINT(FREQBYNUM):   PROC SQL;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
SYMBOLGEN:  Macro variable NUM resolves to 3
MPRINT(FREQBYNUM):   CREATE TABLE TEMP AS SELECT * FROM varnames(obs=3);
NOTE: Table WORK.TEMP created, with 2 rows and 2 columns.

MPRINT(FREQBYNUM):   QUIT;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.03 seconds


MPRINT(FREQBYNUM):   run;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   PROC PRINT data = TEMP;
MPRINT(FREQBYNUM):   RUN;

NOTE: There were 2 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.05 seconds
      cpu time            0.00 seconds


MLOGIC(FREQBYNUM):  Ending execution.
MPRINT(LOOP):  ;
MLOGIC(LOOP):  %DO loop index variable I is now 4; loop will iterate again.
MLOGIC(FREQBYNUM):  Beginning execution.
SYMBOLGEN:  Macro variable I resolves to 4
MLOGIC(FREQBYNUM):  Parameter DATA has value TEMP
MLOGIC(FREQBYNUM):  Parameter NUM has value 4
MLOGIC(FREQBYNUM):  %LOCAL  VARLIST
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   proc transpose data=TEMP(obs=0) out=varnames;
MPRINT(FREQBYNUM):   var _all_;
MPRINT(FREQBYNUM):   run;

NOTE: There were 0 observations read from the data set WORK.TEMP.
NOTE: The data set WORK.VARNAMES has 2 observations and 2 variables.
NOTE: PROCEDURE TRANSPOSE used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


MPRINT(FREQBYNUM):   PROC SQL;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
SYMBOLGEN:  Macro variable NUM resolves to 4
MPRINT(FREQBYNUM):   CREATE TABLE TEMP AS SELECT * FROM varnames(obs=4);
NOTE: Table WORK.TEMP created, with 2 rows and 2 columns.

MPRINT(FREQBYNUM):   QUIT;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


MPRINT(FREQBYNUM):   run;
SYMBOLGEN:  Macro variable DATA resolves to TEMP
MPRINT(FREQBYNUM):   PROC PRINT data = TEMP;
MPRINT(FREQBYNUM):   RUN;

NOTE: There were 2 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.04 seconds
      cpu time            0.00 seconds


MLOGIC(FREQBYNUM):  Ending execution.
MPRINT(LOOP):  ;
MLOGIC(LOOP):  %DO loop index variable I is now 5; loop will not iterate again.
MLOGIC(LOOP):  Ending execution.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The Full Log&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 03:52:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307303#M65794</guid>
      <dc:creator>Marzi</dc:creator>
      <dc:date>2016-10-26T03:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307337#M65804</link>
      <description>&lt;P&gt;I totally agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;&amp;nbsp;here, your code doesn't really do any that this wouldn't do in a far more succient way:&lt;/P&gt;
&lt;PRE&gt;data want;
  set sashelp.vcolumn (where=(libname="WORK" and memname="TEMP") keep=name);
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 26 Oct 2016 08:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307337#M65804</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-10-26T08:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307828#M65991</link>
      <description>Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; I need to call variables by their position, rather than their name</description>
      <pubDate>Fri, 28 Oct 2016 03:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307828#M65991</guid>
      <dc:creator>Marzi</dc:creator>
      <dc:date>2016-10-28T03:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307836#M65995</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/108727"&gt;@Marzi﻿&lt;/a&gt;&amp;nbsp;Please look at the SASHELP.VCOLUMN website. There's a variable that contains the order of the variables, I can't recall the name now. Add a clause to &amp;nbsp;the query&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and order &amp;lt; &amp;amp;var_nums&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2016 04:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307836#M65995</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-28T04:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: call variable names in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307840#M65996</link>
      <description>&lt;P&gt;Thank &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza﻿&lt;/a&gt;, it was helpful.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2016 05:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-variable-names-in-a-loop/m-p/307840#M65996</guid>
      <dc:creator>Marzi</dc:creator>
      <dc:date>2016-10-28T05:11:06Z</dc:date>
    </item>
  </channel>
</rss>

