BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Wub_SAS
Obsidian | Level 7

Hi Everyone,

 

I was wondering if there is any a more efficient way of getting out all list of the variables in a SAS dataset together at once instead of manually copying and pasting each variable individually (preferably string and numeric separately). This is because I need to convert thousands of hundreds of string variables to numeric as part of data cleaning and doing it manually is very time consuming.

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

You can put them into macro variables like this (for SASHELP:CLASS, change the LIBNAME and MEMNAME for other tables):

proc sql noprint;                                                                                                                       
  select name into :charvars separated by ' ' from dictionary.columns                                                                   
    where libname='SASHELP' and memname='CLASS' and type='char';                                                                        
  select name into :numvars separated by ' ' from dictionary.columns                                                                    
    where libname='SASHELP' and memname='CLASS' and type='num';                                                                         
quit;                                                                                                                                   
                                                                                                                                        
%put &charvars;                                                                                                                         
%put &numvars;                                                                                                                          

View solution in original post

3 REPLIES 3
s_lassen
Meteorite | Level 14

You can put them into macro variables like this (for SASHELP:CLASS, change the LIBNAME and MEMNAME for other tables):

proc sql noprint;                                                                                                                       
  select name into :charvars separated by ' ' from dictionary.columns                                                                   
    where libname='SASHELP' and memname='CLASS' and type='char';                                                                        
  select name into :numvars separated by ' ' from dictionary.columns                                                                    
    where libname='SASHELP' and memname='CLASS' and type='num';                                                                         
quit;                                                                                                                                   
                                                                                                                                        
%put &charvars;                                                                                                                         
%put &numvars;                                                                                                                          

Wub_SAS
Obsidian | Level 7
Thank you very much! That was useful.
Patrick
Opal | Level 21

@Wub_SAS "This is because I need to convert thousands of hundreds of string variables to numeric"

That feels wrong. Are you sure you're dealing with a SAS table that got that many variables? Or are you talking about individual data points?

How do you get all these character variables in first place? Is that something you're doing and could eventually influence/change earlier in the process?

 

I suggest you provide a bit more information what you have and what you need and we can eventually give you more targeted advice. 

 

If you really got a lot of variables that you need to change to numeric and these variable names follow some pattern AND the strings really contain all digits that can get converted then it might be easiest to generate the full code for this conversion. But we can only say if you tell us more. And if you're after actual code as solution proposition then provide representative sample data via a fully working and tested SAS data step that you post here (just a few rows and variables that are representative for your real data).

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 499 views
  • 0 likes
  • 3 in conversation