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

Any function to tell a column at a dataset is numeric or not?!

 

Reading in files, but some columns are numbers but are readin as char.

So need a macro take dataset name and column name 1) tells numeric or not 2) if not, simple to 

change the data format. 

 

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@hellohere wrote:

Any function to tell a column at a dataset is numeric or not?!

 

Reading in files, but some columns are numbers but are readin as char.

So need a macro take dataset name and column name 1) tells numeric or not 2) if not, simple to 

change the data format. 

 

Thanks,


Such programs have been written.  Here is one https://github.com/sasutils/macros/blob/master/varexist.sas

 

So you could use that macro to find the TYPE of the vairable.

%put AGE in SASHELP.CLASS is %varexist(sashelp.class,age,type);

But you might want to take a step back and find out why the variable was not created properly.  Where did the dataset in question come from?  How did it get into SAS? 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

PROC CONTENTS will tell you (and there are other ways as well)

 

proc contents data=sashelp.class;
run;

PaigeMiller_0-1721218443247.png

 

 

So need a macro take dataset name and column name 1) tells numeric or not 2) if not, simple to 

change the data format. 

 

This is not a "format" issue, it is a variable type issue. In general, if the column is not numeric, you can't convert it to numeric unless it contains only digits (and perhaps a single decimal point and perhaps a single minus sign at the start of the text string). You can't convert these two character variables to numeric. So what do you really want here?

 

--
Paige Miller
ballardw
Super User

One way to query specific data set properties:

proc sql;
   select name, type 
   from dictionary.columns
   where libname='SASHELP' and memname='CLASS' 
      and upcase(name)='SEX'
   ;
quit;

Libname and memname (dataset/view /etc ) are stored in uppercase. If you have more than datasets in your libarary you may need to add " and memtype='DATA' ".

The names of the variables may not be of consistent case so address that with one of the case functions.

If you have a list of variables to check then use something like: " upcase(name) in ('VAR1' 'VARNAME2' 'OTHERVARNAME')".

Or skip the name entirely essentially duplicating Proc Contents output...

 

Or look at the documentation of the VARTYPE function as an alternate.

Reeza
Super User

@hellohere wrote:

 

 

Reading in files, but some columns are numbers but are readin as char.

 


If you can fix it while you read in the data that's preferable than fixing after the fact. In the odd case you read in a variable as numeric instead of character you would lose information. If all files are the same layout using a data step to read rather than proc import will help to standardize the read in and fix it. 

Tom
Super User Tom
Super User

@hellohere wrote:

Any function to tell a column at a dataset is numeric or not?!

 

Reading in files, but some columns are numbers but are readin as char.

So need a macro take dataset name and column name 1) tells numeric or not 2) if not, simple to 

change the data format. 

 

Thanks,


Such programs have been written.  Here is one https://github.com/sasutils/macros/blob/master/varexist.sas

 

So you could use that macro to find the TYPE of the vairable.

%put AGE in SASHELP.CLASS is %varexist(sashelp.class,age,type);

But you might want to take a step back and find out why the variable was not created properly.  Where did the dataset in question come from?  How did it get into SAS? 

hellohere
Pyrite | Level 9
Thanks all, The macro should work out well.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 232 views
  • 0 likes
  • 5 in conversation