As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Difference between revisions of "Tips:Easy Way to Get All Variable Names From a Dataset"

From sasCommunity
Jump to: navigation, search
(gardening)
(gardening)
Line 14: Line 14:
 
This creates a macro variable called <big><source lang="sas" enclose="none">&vlist</source></big> that will contain the names of all the variables in your dataset, separated by a space.  If you want commas between the variable names, all you have to do is change the 'separated by' value from ' ' to ', '.  The use of the upcase function in the where statement avoids problems with someone passing the dataset name in the wrong case.  The global statement is needed since the macro variable created will not necessarily be available outside the macro without defining it as global.
 
This creates a macro variable called <big><source lang="sas" enclose="none">&vlist</source></big> that will contain the names of all the variables in your dataset, separated by a space.  If you want commas between the variable names, all you have to do is change the 'separated by' value from ' ' to ', '.  The use of the upcase function in the where statement avoids problems with someone passing the dataset name in the wrong case.  The global statement is needed since the macro variable created will not necessarily be available outside the macro without defining it as global.
  
{{ExternalReadMore|http://web.archive.org/web/20150429224020/http://support.sas.com/kb/25/083.html}} (especially the Full Code tab)
+
{{clear}}...[http://web.archive.org/web/20150429224020/http://support.sas.com/kb/25/083.html see also] (especially the Full Code tab)
 
+
 
{{SubmittedBy|Spm}}
 
{{SubmittedBy|Spm}}
  

Revision as of 12:52, 13 September 2017

In the recent issue of SAS TechReport, there was a tip for using %sysfunc to get all the variable names from a dataset and store them in a single macro variable. In the referenced sample code there is an alternate way using the dictionary tables:

%macro getvars(dsn);
    %global vlist;
    proc sql;
        select name into :vlist separated by ' '
        from dictionary.columns
        where memname = upcase("&dsn");
    quit;
%mend;

This creates a macro variable called &vlist that will contain the names of all the variables in your dataset, separated by a space. If you want commas between the variable names, all you have to do is change the 'separated by' value from ' ' to ', '. The use of the upcase function in the where statement avoids problems with someone passing the dataset name in the wrong case. The global statement is needed since the macro variable created will not necessarily be available outside the macro without defining it as global.


...see also (especially the Full Code tab)

Submitted by Spm. Contact me at my Discussion Page.