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

the way date is stored in SAS as that it starts from 1/1/1960. So it is not fitting in your calculation. So when you are calculating with date it does not work. below thing may work

 
data want;
set want;
numeric=put(ex_post_date,best8.);
run;


data wantfinal;
set want;
year=year(numeric);
month=month(numeric);
day=day(numeric);
NEED_num=mdy(month,day,year);
format NEED_num mmddyy10.;
drop year month day;
run;
Yegen
Pyrite | Level 9

@kiranv_, thanks for your reply. The code didn't work, unfortunately, work. However, @Reeza's suggestion works well. 

Thanks to you both again.

Reeza
Super User

Please review how SAS stores date variables. It counts it as the number of days. So that num_needed is actually what you got from the INPUT() from @Toms answer. If you want day/month/year, use those respective functions on the date. 

 

Please run the following and then look at the proc contents of the data set, types/formats. 

 

Data sample;
D1 = '01Jan2009';* -> character;

D2 = input(D1, DATE9.); *SAS date;
D3 = D2; *SAS date, formatted;

Format D3 ddmmyy10.;

M = month(d2);
D = day(d2);
Y = year(d2);

Run;
Yegen
Pyrite | Level 9

@Reeza, thanks for this helpful suggestion. The code works very well. You are right in regards to being careful how SAS stores date variables. I will look into it now.


Thanks again. 

Tom
Super User Tom
Super User

You are still not getting the difference between a character variable and a numeric variable and the meaning of a format.  The format is just instructions on how to display value.  The PUT() function will always return character strings. Then INPUT() function can return either numeric or character values based on the type of INFORMAT used as the second argument.  There is a BEST format that will try to fit a number into the width specified. THere is isn't really a BEST informat, but if you use it then SAS will silently convert to using the normal numeric informat which handles and values that the BEST format would write.

 

Let's rewrite your posted code using variable names that will help understand what they contain.

So let's start with a character variable that has strings that look like dates in DATE9 format.  We can use the INPUT() function to convert that to a date.  We can make copies of it into other variables so we can apply different formats to them to see how they print.  We could use an INPUT(PUT()) sandwich to convert the date to a number with the days in the ones and tens places.  We can use the MONTH(),DAY() and YEAR() functions to convert a date value into its parts. Or we could use arthimetic to convert a number that is in the format YY,YYM,MDD into the year, month and day values.  We can use the MDY() function to generate a date value from month day and year values.

data have ;
  input char_date $9.;
cards;
01JAN1960
14JUN2017
;

data want ;
  set have (keep=char_date);
  date1 = input(char_date,date9.);
  date2 = date1;
  date_unformatted=date1;
  format date1 date9. date2 yymmddn8.;
  number = input(put(date1,yymmddn8.),8.);
  year1=year(date1);
  month1=month(date1);
  day1=day(date1);
  year2=int(number/10000);
  month2=int(mod(number,10000)/100);
  day2=mod(number,100);
  new_date=mdy(month1,day1,year1);
  format char_date $quote. ;
  format date1 date9. date2 yymmddn8. new_date mmddyy10.;
  format number date_unformatted comma10.;
run;

I purposely using the $QUOTE and COMMA formats so that when you print the values you can tell which variables are character and non-date numbers.

Capture.PNG

 

 

Yegen
Pyrite | Level 9

@Tom, thank you so much for taking your time to go over how formatting of variables works in SAS. I have stopped using SAS for a long time and just moved a few weeks ago back. I was not familiar with formatting of variables, but this reply was really helpful! Now I should be good with formatting! Thanks again.


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 20 replies
  • 61167 views
  • 11 likes
  • 6 in conversation