BookmarkSubscribeRSS Feed
marleeakerson
Calcite | Level 5

Hello, 

 

I need some help converting a numeric variable into a SAS date variable. 

 

I am trying to get the last day of the month into a new variable. The original variable is "Date_Collect" which is a date variable, then I used the intnx function to grab the last day of the month. The step I am struggling with is converting the lastday variable into a SAS date variable instead of a numeric variable. 

 

When I run this code, lastday_date is entirely blank.

 

Any ideas? 

 

 

data want;
set have;
lastday_num=intnx('month',Date_Collect,0,'E');
lastday_date=input(put(lastday_num,date8.),yymmdd8.);
  format lastday_date date8.;
run;
3 REPLIES 3
mklangley
Lapis Lazuli | Level 10

@marleeakerson  Is this what you're looking for? All you need is a format to display the numeric date as a date value. Here is an example:

data example;
    lastday_num = intnx('month', "09Sep2020"d, 0, 'E');
    lastday_date = lastday_num;
    format lastday_date date8.;
run;

  So you should just be able to use this:

data want;
    set have;
    lastday_num = intnx('month', Date_Collect, 0, 'E');
    lastday_date = lastday_num;
    format lastday_date date8.;
run;
Cynthia_sas
SAS Super FREQ

Hi:

  I'm not clear on what you're trying to do. INTNX is moving a date forward or backward in time. A SAS date value is ALREADY a numeric value. It represents the number of days either before or after Jan 1, 1960 which is internally stored as 0. So Jan 2, 1960 is stored as 1; Jan 3, 1960 is stored as 2; Dec 31, 1959 is stored as -1, etc. A date like Nov 15, 2019 would be stored internally as the number 21868, because it is 21868 days after Jan 1, 1960. So if you use INTNX to MOVE to the date Nov 30, 2019, then the internally stored number for THAT moved value would be 21883.

  Do you want to create a character version of your Date_Collect value? INTNX is already creating a numeric variable for you with the value you want. You can run this program to test:

data testnx;
  infile datalines;
  input Date_Collect : mmddyy.;
  lastday_num=intnx('month',Date_Collect,0,'E');
  putlog '=== === === ===' _n_=  date_collect= date_collect= date9. ' === === === === ===';
  putlog '===> ' lastday_num= '(this is internally stored number)';
  putlog '===> ' lastday_num= mmddyy10. '(this is formatted value)';
  putlog '===> ' lastday_num= date9. '(this is alternative format)';
return;
datalines;
11/15/2019
03/21/2020
;
run;

When I run the above program, this is what I see in the LOG:

Cynthia_sas_0-1600450226756.png

Can you clarify what it is you need to do???

Cynthia

ballardw
Super User

@marleeakerson wrote:

Hello, 

 

I need some help converting a numeric variable into a SAS date variable. 

 

I am trying to get the last day of the month into a new variable. The original variable is "Date_Collect" which is a date variable, then I used the intnx function to grab the last day of the month. The step I am struggling with is converting the lastday variable into a SAS date variable instead of a numeric variable. 

 

When I run this code, lastday_date is entirely blank.

 

Any ideas? 

 

 

data want;
set have;
lastday_num=intnx('month',Date_Collect,0,'E');
lastday_date=input(put(lastday_num,date8.),yymmdd8.);
  format lastday_date date8.;
run;

Besides NOT needing

lastday_date=input(put(lastday_num,date8.),yymmdd8.);

because lastday_date is a date value, you are making a value that cannot be read with yymmdd8. When put a date with date8. format then you get a value with a leading space that looks like " 01SEP20". YYMMDD8 expects numerals that represent year month and day, like 20200901 (for 01SEP2020).

The leading space created by DATE8 means almost any informat would fail.

 

If you find someone using code that has any code that uses: input(put(variable,<some date format>), <some date informat>)

then do NOT copy that. The variable is Already a date value and changes in appearance just require a Format assignment or the variable is not a date and will yield unexpected results.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 516 views
  • 0 likes
  • 4 in conversation