Help using Base SAS procedures

Character to numeric

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Character to numeric

I know this is going to sound so ridiculous, but bear with me, I'm fairly new to SAS and please in answer I am looking for example code. I have a dataset that I am preparing to do a linkage on. The date format is wrong for a variable it is a character defined variable with the numeric string formatted MMDDYYYY. I need to convert it to numeric so that this code below will work for converting the date. How do I convert the variable to numeric so I can use the code below? 

 

data set;
set set;
DOB=put(DOB,yymmdd8.);
run;


Accepted Solutions
Solution
‎04-26-2016 08:20 AM
Super User
Super User
Posts: 6,153

Re: Character to numeric

[ Edited ]

Your code is for creating a character variable from a date value.  So if your original variable is also character then you just add an INPUT() function call to convert it to a date so the PUT() function will work.

 

data WANT;
  set HAVE;
  DOB=put(input(DOB,mmddyy8.),yymmddN8.);
run;

Also if you want the result ot look like YYYYMMDD (with no delimiters) then use the yymmddN format.

View solution in original post


All Replies
Solution
‎04-26-2016 08:20 AM
Super User
Super User
Posts: 6,153

Re: Character to numeric

[ Edited ]

Your code is for creating a character variable from a date value.  So if your original variable is also character then you just add an INPUT() function call to convert it to a date so the PUT() function will work.

 

data WANT;
  set HAVE;
  DOB=put(input(DOB,mmddyy8.),yymmddN8.);
run;

Also if you want the result ot look like YYYYMMDD (with no delimiters) then use the yymmddN format.

Occasional Contributor
Posts: 9

Re: Character to numeric

Thank you! I almost had it right, but was missing just a small part. This is perfect! 

Esteemed Advisor
Posts: 6,309

Re: Character to numeric

Please give @Tom credit by marking his answer as the solution.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 301 views
  • 1 like
  • 3 in conversation