spioter
spioter

Reputation: 1870

SAS VARTYPE Function: run it (or equivalent) against all Variables

I am a DB administrator with 0 SAS experience and I work in government and have been tasked with ingesting SAS output from another team. The other team has limited SAS experience apparently and cannot answer the question "what is the data type of each SAS variable". We have dozens of tables and thousands of variables to import. Is there a way to run the SAS function "VarType" against all columns?

I've not found what I needed on SAS docs, SO search, etc.

I am expecting code that I can hand to the other team which they will run to produce the following (with only hard-coding the "dataset" ; no hard-coded table names/variable names):

TableName VariableName DataType DataLength and/or Other attributes as needed
MyTable 1 Column1 char 25
MyTable 1 Col2 numeric scale 10 precision 2
MyTable 2 Col1 (small? big? 32? ) int bytes? or something that tells me max range
...
MyTable102 Column100 date yyyy-mm-dd

Update: here's what I used based on the accepted answer. You would change:

proc datasets library=SASHELP  memtype=data;
    contents data=_ALL_ (read=green) out=yourDataset.sasSchemaDump;
    title  'SAS Schema Dump';
    run;

Upvotes: 0

Views: 694

Answers (2)

Tom
Tom

Reputation: 51566

Run PROC CONTENTS on the dataset and you will have the information you need.

SAS has only two data TYPE. Fixed length character strings and floating point numbers. The LENGTH is the number of bytes that are stored in the dataset. So for character variables the length determines how many characters it can store (assuming you are using a single byte encoding). Floating point numbers require 8 bytes to store, but you can store it with fewer in the dataset if you don't mind the loss of precision that means. For example if you know the values are integers you might choose to store only 4 of the bytes.

You can sometimes tell more information about a variable if the creator attach a permanent FORMAT to control how the variable is displayed. For example SAS stores DATE values as the number of days since 1960. So to make those number meaningful to humans you need to attach a format such as DATE9. or YYMMDD10. so that the numbers print as strings that a human would see as a date. Similarly there are display formats for displaying time of day value (number of seconds since midnight) or datetime values (number of seconds since 1960). Also if they attached a format that does not display decimal places that might mean the values are intended to be integers.

And if they attached a LABEL to the variable that might explain more about the variable than you can learn from the name alone.

They could also attach user defined formats to a variable. Those could be simple code/decode lookups, but they could also be more complex. A common complex one is used for collapsing a range (or multiple values and/or ranges) to a single decode. The definition of a user defined format is stored in a separate file, called a catalog, in particular a format catalog. You can use PROC FORMAT with the FMTLIB or CNTLOUT= option to see the definition of the user defined formats.

Upvotes: 1

Kermit
Kermit

Reputation: 3117

There is a dedicated SAS procedure for this: PROC CONTENTS

proc contents data=sashelp.cars out=want; run;

It will create a SAS table want with all the information needed.

FYI: TYPE 1 is numeric, TYPE 2 is character.

If all tables are in the same library you could do the following to cycle through all the tables within the library

proc contents data=sashelp._all_ out=want; run;

Upvotes: 3

Related Questions