BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Meera
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

8 REPLIES 8
Arul
Calcite | Level 5

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;

Reeza
Super User

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

Meera
Calcite | Level 5

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

Reeza
Super User

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;

Meera
Calcite | Level 5

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

ballardw
Super User

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.

Reeza
Super User

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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