<?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: sysfunc get all variable names from dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889687#M351528</link>
    <description>&lt;P&gt;Another option for a function-style macro that returns a list of variables is to use DOSUBL.&amp;nbsp; This allows you to run PROC or DATA steps in the "side session", rather than rely on pure macro code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlist(data) ;
  %local rc varnames ;
  %let rc = %sysfunc(dosubl(%nrstr(
    proc contents data=&amp;amp;data out=__VarList(keep=name) noprint ;
    run ;
    proc sql noprint ;
      select name into :varnames separated by ' '
      from __VarList ;   
      drop table __VarList ;
    quit ;
  )));

&amp;amp;varnames /*return*/
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The nice thing is that you can then use the usual KEEP or DROP option to subset variables, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %varlist(sashelp.class) ;
%put %varlist(sashelp.class(keep=_numeric_)) ;
%put %varlist(sashelp.class(drop=Age--Weight)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To read more about DOSUBL, you'll want to start with Rick Langston's masterful introduction:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings13/032-2013.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/032-2013.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DOSUBL will run more slowly than the %SYSFUNC(open()) approach, because DOSUBL has to do a good bit of overhead work in order to create the side-session.&lt;/P&gt;</description>
    <pubDate>Thu, 17 Aug 2023 15:36:37 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-08-17T15:36:37Z</dc:date>
    <item>
      <title>sysfunc get all variable names from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889605#M351497</link>
      <description>&lt;P&gt;Is it possible to get all variable names from dataset by using %sysfunc() (i mean by using only macro code without datasteps and procedures like SQL")? So i could insert this call as argument for other user's defined macro function.&lt;/P&gt;&lt;P&gt;Probably it has to use %sysfunc(varname) function.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 08:59:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889605#M351497</guid>
      <dc:creator>TimurShangareev</dc:creator>
      <dc:date>2023-08-17T08:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: sysfunc get all variable names from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889614#M351502</link>
      <description>&lt;P&gt;Why do you specify that it has to use %SYSFUNC()? It is counter-productive to place restrictions like this on what solution is allowed. And no data steps or SQL is allowed either? That's like saying to a carpenter, build me a cabinet but you're not allowed to use any kind of saw or drill.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;So i could insert this call as argument for other user's defined macro function.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Again, its hard for me to imagine why this is relevant, arguments can be derived in any way that works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's code that creates a macro variable named VARNAMES that contains the names of variables in data set SASHELP.CLASS:&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 :varnames separated by ' ' from dictionary.columns where libname='SASHELP' and memname='CLASS';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;amp;varnames can be an argument to someone else's macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 10:07:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889614#M351502</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-17T10:07:39Z</dc:date>
    </item>
    <item>
      <title>Re: sysfunc get all variable names from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889632#M351506</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_all_varname(dsn=);
%let dsid=%sysfunc(open(&amp;amp;dsn.));
%let nvar=%sysfunc(attrn(&amp;amp;dsid.,nvars));
%do i=1 %to &amp;amp;nvar.;
  %let vname=%sysfunc(varname(&amp;amp;dsid.,&amp;amp;i.));
  %put &amp;amp;=vname.;
%end;
%let dsid=%sysfunc(close(&amp;amp;dsid.));
%mend;

%get_all_varname(dsn=sashelp.heart)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Aug 2023 11:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889632#M351506</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-08-17T11:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: sysfunc get all variable names from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889642#M351511</link>
      <description>&lt;P&gt;Someone has a utility macro that does this that also allow filtering that they have posted multiple times.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in general just open the dataset and loop over the set of variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro listvar(ds);
%local dsid i;
%let dsid=%sysfunc(open(&amp;amp;ds));
%if &amp;amp;dsid %then %do;
  %do i=1 %to %sysfunc(attrn(&amp;amp;dsid,nvars));
 %qsysfunc(varname(&amp;amp;dsid,&amp;amp;i))
  %end;
  %let i=%sysfunc(close(&amp;amp;dsid));
%end;
%mend listvar;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;137  %put |%listvar(sashelp.class)|;
|Name  Sex  Age  Height  Weight|

&lt;/PRE&gt;</description>
      <pubDate>Thu, 17 Aug 2023 13:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889642#M351511</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-17T13:11:39Z</dc:date>
    </item>
    <item>
      <title>Re: sysfunc get all variable names from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889687#M351528</link>
      <description>&lt;P&gt;Another option for a function-style macro that returns a list of variables is to use DOSUBL.&amp;nbsp; This allows you to run PROC or DATA steps in the "side session", rather than rely on pure macro code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro varlist(data) ;
  %local rc varnames ;
  %let rc = %sysfunc(dosubl(%nrstr(
    proc contents data=&amp;amp;data out=__VarList(keep=name) noprint ;
    run ;
    proc sql noprint ;
      select name into :varnames separated by ' '
      from __VarList ;   
      drop table __VarList ;
    quit ;
  )));

&amp;amp;varnames /*return*/
%mend ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The nice thing is that you can then use the usual KEEP or DROP option to subset variables, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put %varlist(sashelp.class) ;
%put %varlist(sashelp.class(keep=_numeric_)) ;
%put %varlist(sashelp.class(drop=Age--Weight)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To read more about DOSUBL, you'll want to start with Rick Langston's masterful introduction:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings13/032-2013.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/032-2013.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DOSUBL will run more slowly than the %SYSFUNC(open()) approach, because DOSUBL has to do a good bit of overhead work in order to create the side-session.&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 15:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889687#M351528</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-17T15:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: sysfunc get all variable names from dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889692#M351530</link>
      <description>Are you intending to use this within the data step itself on the same dataset?&lt;BR /&gt;&lt;BR /&gt;If so, may be worth exploring call vnext a bit. &lt;BR /&gt;</description>
      <pubDate>Thu, 17 Aug 2023 15:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sysfunc-get-all-variable-names-from-dataset/m-p/889692#M351530</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-17T15:49:11Z</dc:date>
    </item>
  </channel>
</rss>

