Help using Base SAS procedures

Retrieve date month and year in seperate variables

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Retrieve date month and year in seperate variables

How do I retrieve date, month and year in seperate variables. I have to use a if condition.. and i need those values to be stored in a variable seperately.

Thanks.


Accepted Solutions
Solution
‎05-16-2013 01:13 PM
Super User
Super User
Posts: 6,157

Re: Retrieve date month and year in seperate variables

Display the values in DDMONYYYY format. If there are ANS for date and month fields, display them as ANS/ANS/YYYY.

This is my scenario. My date variable is stored as character and yyyy/mm/dd format. Can you help me? Let me know if you need more info

You will probably need to run code to do this, rather than create a format.

Let's assume your existing character variable is named DATE and the variable you want to create for your report is named DATE2.

data want ;

   set have;

   if index(date,'ANS') then date2 = 'ANS/ANS/' || substr(date,1,4) ;

  else date2 = put( input( date, yymmdd10.), date9.) ;

run;

View solution in original post


All Replies
New Contributor
Posts: 2

Re: Retrieve date month and year in seperate variables

You could try date functions as below


data _null_;

dob='19may2013'd;

day=day(dob);

month=month(dob);

year=year(dob);

put dob day month year;

run;

Grand Advisor
Posts: 16,926

Re: Retrieve date month and year in seperate variables

Why do you want to separate it and how is your date stored?

New Contributor
Posts: 3

Re: Retrieve date month and year in seperate variables

I have a dataset with a date variable (yyyy/mm/dd). There are 30 rows for that variable. I have to generate a report to check each row and see if date and mon are specific value then display a text if not display something else. I am new to sas.. so I would appreciate any feedback from you. I am thinking of using Substr function. One of my friend used macro and used a substr and i think its too complex. I am trying to use sas code efficiently.

Thanks

Grand Advisor
Posts: 16,926

Re: Retrieve date month and year in seperate variables

I recommend reading this paper:

http://www.lexjansen.com/pharmasug/2005/tutorials/tu01.pdf

Also, you should verify if it is a date variable or a character variable.

Run a proc contents on the data and examine the results for the variable of interest.

Usually it's better to use the built in SAS date functions rather than separate it out if possible, but you haven't provided enough information to comment on that.

proc contents data=have;

run;

New Contributor
Posts: 3

Re: Retrieve date month and year in seperate variables

Display the values in DDMONYYYY format. If there are ANS for date and month fields, display them as ANS/ANS/YYYY.


This is my scenario. My date variable is stored as character and yyyy/mm/dd format. Can you help me? Let me know if you need more info

Grand Advisor
Posts: 10,066

Re: Retrieve date month and year in seperate variables

Create a SAS date variable first using:

NewDateVar = input(yourdatecharactervarname, yymmdd10.);

Format NewDateVar ddmonyy9.;

then use Arul's approach.

BTW, if the date is not a valid date then NewDateVar above will be missing and you'll warnings in the log.

I'm not sure what you mean by ANS.

Grand Advisor
Posts: 16,926

Re: Retrieve date month and year in seperate variables

If you're trying to deal with dates that have missing parts (e.g missing day or month) then I think you'll need to roll your own format.

The ideal solution would be to create a format that would handle the missing portions, but I'm not aware of how to do that.

The brute force methodology is:

to substr to partition the dates into the day/month/year.

Map month number to 3 digit short name.

Check for missing fields.

If missing the create char as first format, if not then put in other format.

Here's a presentation on getting some of that, and the paper above would be useful:

http://www.sas.com/offices/NA/canada/downloads/UserGroups/eSUG-May2013/Eberhardt-PerlPPT.pdf

Solution
‎05-16-2013 01:13 PM
Super User
Super User
Posts: 6,157

Re: Retrieve date month and year in seperate variables

Display the values in DDMONYYYY format. If there are ANS for date and month fields, display them as ANS/ANS/YYYY.

This is my scenario. My date variable is stored as character and yyyy/mm/dd format. Can you help me? Let me know if you need more info

You will probably need to run code to do this, rather than create a format.

Let's assume your existing character variable is named DATE and the variable you want to create for your report is named DATE2.

data want ;

   set have;

   if index(date,'ANS') then date2 = 'ANS/ANS/' || substr(date,1,4) ;

  else date2 = put( input( date, yymmdd10.), date9.) ;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 1432 views
  • 0 likes
  • 5 in conversation