Converting dates from external file

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 120
Accepted Solution

Converting dates from external file

[ Edited ]

I have the data in the attached .csv file; the Date variable containes future dates for the appropriate Index. The Date variable gets imported into SAS as a character variable after I use the appropriate proc.

 

I would like to either a) import the variable into SAS as a numeric variable or b) convert the variable into numeric after I have imported it into SAS. Also, I would appreciate any thoughts on how I can deal with the issue that some of my values in the Date variable show up as mmm-yy, rather than yy-mmm.

 

I would appreciate any insight that you may have to offer.

 

Thanks in advance.

Attachment

Accepted Solutions
Solution
‎03-10-2017 04:30 PM
Esteemed Advisor
Posts: 6,646

Re: Converting dates from external file

You can only read into a character variable; next use the position of the hyphen (determine with indexc()) to decide how to correct the order and drop the hyphen with substr(), so all entries are structured as MMMYY. After prepending "01" as day you can use input(chardate,date7.) to convert to a SAS date.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎03-10-2017 04:30 PM
Esteemed Advisor
Posts: 6,646

Re: Converting dates from external file

You can only read into a character variable; next use the position of the hyphen (determine with indexc()) to decide how to correct the order and drop the hyphen with substr(), so all entries are structured as MMMYY. After prepending "01" as day you can use input(chardate,date7.) to convert to a SAS date.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 74

Re: Converting dates from external file

[ Edited ]

I would import them as character then deal with them in SAS.  You can check the values and convert them depending on the source value. For instance you can check if the first character is numeric then create a date using the first value* as the year, and the second** as the month in the MDY function.  Conversely if the first character is a letter, use the first value as the month and the second as the year in the same function.  You'd have to supply a value for the day as mentioned in the previous answer. 

 

* scan(date, 1, '-');

** scan(date, 2, '-');

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 155 views
  • 1 like
  • 3 in conversation