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

/*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

*/

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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;

View solution in original post

6 REPLIES 6
stat_sas
Ammonite | Level 13

data t;

input open_date :anydtdte. age;

cards;

30Sep2011 7

01Dec2014 2

;

run;

Mirisage
Obsidian | Level 7

Hi stat@sas,

Thanks.

However, when I ran your code, it produced the below dataset.

open_dateage
189007
200582

But What I really wanted is like below.

Date_as_a_Value

age
2011097
2014122

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

stat_sas
Ammonite | Level 13

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;

Mirisage
Obsidian | Level 7

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

stat_sas
Ammonite | Level 13

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.

Mirisage
Obsidian | Level 7

Thanks again stat@sas for this knowledge.

Mirisa

sas-innovate-2024.png

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.

 

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
  • 6 replies
  • 1153 views
  • 0 likes
  • 2 in conversation