Help using Base SAS procedures

Question: How to change formats for an existing SAS data set.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Question: How to change formats for an existing SAS data set.

I know this answer is out there, but what I find via Google is how to format when importing data that is not already in SAS form. I really can't force myself to read a 10 page PDF (man, SAS is longwinded) for such a simple problem.

Anyway, here are the contents of my data:

                          Alphabetic List of Variables and Attributes

              #    Variable     Type    Len    Format       Informat    Label

              1    BondCusip    Char      8    $8.          $8.         BondCusip

              9    BondPrice    Num       8

              2    CPN          Num       8                             CPN

              4    CPN_FREQ     Num       8                             CPN_FREQ

              7    Cvx          Num       8

              6    Date         Num       8    MMDDYY10.

              8    Dur          Num       8

              5    ISSUE_DT     Char     21    $21.         $21.        ISSUE_DT

              3    MATURITY     Char     21    $21.         $21.        MATURITY

I need Issue_dt and maturity to be numeric and formatted as mmddyy10. Any help is always appreciated.


Accepted Solutions
Solution
‎08-03-2011 12:57 PM
Trusted Advisor
Posts: 2,114

Question: How to change formats for an existing SAS data set.

To convert a text date to numeric, use the INPUT function, as in

maturityD=INPUT(maturity, anydtdte21.);

anydtdte will read a variety of date and date-time formats and extract the date (only) part.

To add the format,

FORMAT maturityd mmddyy10.;

SAS cannot change a data type from character to numeric in place; you have to change the variable's name to do it.

Doc Muhlbaier

Duke

View solution in original post


All Replies
PROC Star
Posts: 7,416

Question: How to change formats for an existing SAS data set.

What does the data look like?  From the size of the field, I would guess they contain datetime data.

Anyhow, what you have to do is create two new fields, probably using the input and datepart functions, the anydtdtmw informat, and a format statement.  However, I'm hesitant to suggest specific code without seeing an example of the data the two fields contain.

Solution
‎08-03-2011 12:57 PM
Trusted Advisor
Posts: 2,114

Question: How to change formats for an existing SAS data set.

To convert a text date to numeric, use the INPUT function, as in

maturityD=INPUT(maturity, anydtdte21.);

anydtdte will read a variety of date and date-time formats and extract the date (only) part.

To add the format,

FORMAT maturityd mmddyy10.;

SAS cannot change a data type from character to numeric in place; you have to change the variable's name to do it.

Doc Muhlbaier

Duke

Occasional Contributor
Posts: 16

Question: How to change formats for an existing SAS data set.

So that's the key. I must create a new variable from the old. I'm so use to Excel that it makes me assume SAS will behave the same way. Thanks a ton!

Super User
Posts: 18,549

Question: How to change formats for an existing SAS data set.

You're trying to change the variable TYPE from CHAR to NUMERIC.

I'm also guessing that your Issue_DT and Maturity are date time fields not date based on the length of 21. The ideal solution is to fix this when you initially get the data and make sure it imports/downloads/reads properly.

To change it at this point you need to give an example of how it looks it the field, is it 03Aug2011 or or 03/08/11 9:54:00.0 ... or something of the sort.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 2776 views
  • 6 likes
  • 4 in conversation