/*Hi SAS forum,
I have a data set like this.*/
data t;
input open_date date9. age;
cards;
30Sep2011 7
01Dec2014 2
;
run;
/*Using the above, I have to create a numeric variable named “Date_as_a_Value” like below.
Variable “Date_as_a_Value” should not be a date value and has to be a “just Nemeric” (becaue I want to use “Date_as_a_Value” for a further several numeric multiplications etc.).
Date_as_a_Value
201109 /*instead of 30Sep2011*/
201412 /*instead of 01Dec2014*/
*/
/*My approach:*/
/*"Open_date" is Displaying the way I wanted, such as 201109, 201412*/
data t;
input open_date date9. age;
Format open_date YYMMN.;
cards;
30Sep2011 7
01Dec2014 2
;
run;
/*Actually I do not know how to move forward to get what I wanted, which is:
Date_as_a_Value
201109 /*instead of 30Sep2011*/
201412 /*instead of 01Dec2014*/
*/
/*Could someone help me.
Thanks
*/
This will generate new_date variable containing open_date as numeric.
data t;
input open_date :anydtdte. age;
new_date=input(put(open_date,YYMMN.),best.);
cards;
30Sep2011 7
01Dec2014 2
;
run;
proc print data=t;
run;
data t;
input open_date :anydtdte. age;
cards;
30Sep2011 7
01Dec2014 2
;
run;
Hi stat@sas,
Thanks.
However, when I ran your code, it produced the below dataset.
open_date | age |
18900 | 7 |
20058 | 2 |
But What I really wanted is like below.
Date_as_a_Value | age |
201109 | 7 |
201412 | 2 |
The "Date_as_a_Value" variable should be a just numeric value like any other numeric value such as 104, or 200 or 1 or 7 or any other numeric number that you can think of.
Thanks
Mirisa
This will generate new_date variable containing open_date as numeric.
data t;
input open_date :anydtdte. age;
new_date=input(put(open_date,YYMMN.),best.);
cards;
30Sep2011 7
01Dec2014 2
;
run;
proc print data=t;
run;
Hi stat@sas,
Your suggestion worked very well and many thanks.
For my learning, I would like to ask 2 questions from your code (permitting your time).
data t;
input open_date :anydtdte. age;
new_date=input(put(open_date,YYMMN.),best.);
cards;
30Sep2011 7
01Dec2014 2
;
run;
1. 1. In yellow place above, you have used “anydtdte.” as the informat of variable “open_date”. But we know by looking at the data set, this informat is date9.
So, is there any special reason to use :anydtdte. instead of date9. and what “anydtdte.” is really doing?
2. 2. when you convert the character var to numeric, you have used green color “numeric informat” called "best.". But we know that informat is 6. because our convered character var always have 6 characters such as 201109, 201412. Is there any special reason to use best. instead of 6.
Thanks
Mirisa
If date values are not following a consistent pattern then using ANYDTDTE. informat makes the life easier but in your problem date9. will do the same.
Best. chooses the best notation. Please consult SAS documentation to have more info.
Thanks again stat@sas for this knowledge.
Mirisa
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.