BookmarkSubscribeRSS Feed
rockerd
Fluorite | Level 6
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.

3 REPLIES 3
DataShare
SAS Employee
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.
data_null__
Jade | Level 19
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]
  • rockerd
    Fluorite | Level 6
    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

    sas-innovate-2024.png

    Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

    Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

     

    Register now!

    What is Bayesian Analysis?

    Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

    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
    • 1033 views
    • 0 likes
    • 3 in conversation