Help using Base SAS procedures

Couting total numeric variables and character variables

Reply
Occasional Contributor
Posts: 12

Couting total numeric variables and character variables

Hi all,

This is my first post in this forum. It is just out of curiousness to learn.

I want to

1. Count how many of the variables in a data set are numeric and how many are character

2. Create a two different files; file 1 containing only numeric variables and file 2 containing only character variables.

I know we can use the function vtype() to find out if a variable is char or numeric ..

But, if you have thousand variables then how would you do it without having to know the name of that variable...


I thought of using an array .. but then an array can be either char or numeric so using an array is out of question I think...


Thank you,

Rockerd.

SAS Employee
Posts: 13

Re: Couting total numeric variables and character variables

Try this,
proc contents data=libname.datasetname out=Column_Name_Type(keep = NAME TYPE) noprint;
run;
it will create a dataset with all column names and their types.
Respected Advisor
Posts: 3,777

Re: Couting total numeric variables and character variables

You will want to consult the documentation for "SAS Variable Lists".
http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#/documentation/cdl/e...

> 1. Count how many of the variables in a data set are
> numeric and how many are character

You can use PROC CONTENTS as suggested or the dictionary tables or in a data step as

[pre]
548 data _null_;
549 if 0 then set sashelp.shoes;
550 array _n
  • _numeric_;
    551 array _c
  • _char_;
    552 n=dim(_n);
    553 c=dim(_c);
    554 putlog 'NOTE: ' n= c=;
    555 stop;
    556 run;

    NOTE: n=4 c=3
    [/pre]


    > 2. Create a two different files; file 1 containing
    > only numeric variables and file 2 containing only
    > character variables.
    >
    > I know we can use the function vtype() to find out if
    > a variable is char or numeric ..
    >
    > But, if you have thousand variables then how would
    > you do it without having to know the name of that
    > variable...

    Again using a "SAS Variable List".

    [pre]
    580 data num(keep=_numeric_) chr(keep=_char_);
    581 set sashelp.class;
    582 run;

    NOTE: There were 19 observations read from the data set SASHELP.CLASS.
    NOTE: The data set WORK.NUM has 19 observations and 3 variables.
    NOTE: The data set WORK.CHR has 19 observations and 2 variables.

    [/pre]
  • Occasional Contributor
    Posts: 12

    Re: Couting total numeric variables and character variables

    Thank you for the reply.

    I had used _numeric_ in the past but never realized it could be used here as well

    I guess your solution is a good way to get around this
    Ask a Question
    Discussion stats
    • 3 replies
    • 284 views
    • 0 likes
    • 3 in conversation