Help using Base SAS procedures

How to transform 30Sep2011 to read as just a numeric 201109?

Accepted Solution Solved
Reply
Super Contributor
Posts: 338
Accepted Solution

How to transform 30Sep2011 to read as just a numeric 201109?

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

*/


Accepted Solutions
Solution
‎08-25-2014 01:38 PM
Trusted Advisor
Posts: 1,228

Re: How to transform 30Sep2011 to read as just a numeric 201109?

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


All Replies
Trusted Advisor
Posts: 1,228

Re: How to transform 30Sep2011 to read as just a numeric 201109?

data t;

input open_date :anydtdte. age;

cards;

30Sep2011 7

01Dec2014 2

;

run;

Super Contributor
Posts: 338

Re: How to transform 30Sep2011 to read as just a numeric 201109?

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

Solution
‎08-25-2014 01:38 PM
Trusted Advisor
Posts: 1,228

Re: How to transform 30Sep2011 to read as just a numeric 201109?

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;

Super Contributor
Posts: 338

Re: How to transform 30Sep2011 to read as just a numeric 201109?

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

Trusted Advisor
Posts: 1,228

Re: How to transform 30Sep2011 to read as just a numeric 201109?

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.

Super Contributor
Posts: 338

Re: How to transform 30Sep2011 to read as just a numeric 201109?

Thanks again stat@sas for this knowledge.

Mirisa

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 274 views
  • 0 likes
  • 2 in conversation