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.
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;
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;
Why do you want to separate it and how is your date stored?
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
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;
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
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.
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
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.