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

Hello!

 

I have a date field in a numeric format as yyyymm (201603) and I'm trying to convert it to a date format ('01MAR2016'). The day doesn't matter as much because I'm only using this to be able to get 2016-03. And the only way I've found to get that format is by using a put statement for a date field. 

 

Any help is greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So you have a number like 201,603 and you want to interpret it as some day in March of 2016?

You can convert it to a character string and then you could use an INFORMAT to convert it to a date value.

You could then attach the YYMMD7. format specification to it to make it display in YYYY-MM style.

data have;
  input number ;
cards;
201603
;

data want;
   set have;
   date_value = input(put(NUMBER,z6.),yymmn6.);
   format date_value yymmd7. ;
run;

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

So you have a number like 201,603 and you want to interpret it as some day in March of 2016?

You can convert it to a character string and then you could use an INFORMAT to convert it to a date value.

You could then attach the YYMMD7. format specification to it to make it display in YYYY-MM style.

data have;
  input number ;
cards;
201603
;

data want;
   set have;
   date_value = input(put(NUMBER,z6.),yymmn6.);
   format date_value yymmd7. ;
run;
Sas_noob25
Obsidian | Level 7

I tried this as well and I'm still getting blanks for my date2 field.

 

data clean;
set clean;
date2= input(put(date1,z6.),yymmdd6.);
format date2 yymmd7.;
run;

Tom
Super User Tom
Super User

You tried to read the string as if it had YYMMDD values.  But your description said that the values only had YYYYMM values instead.

 

Use the YYMMN informat, not the YYMMDD informat.

 

Or append '01' and use the YYMMDD8. informat instead of YYMMDD6. informat.

ballardw
Super User

@Sas_noob25 wrote:

I tried this as well and I'm still getting blanks for my date2 field.

 

data clean;
set clean;
date2= input(put(date1,z6.),yymmdd6.);
format date2 yymmd7.;
run;


A value of 201603 read with yymmdd6 would treat 20 as the year (i.e. 2020 with likely system settings), 16 as the month (the MM part) and 03 as the day. Since there is no month 16 you would generate an invalid data message similar to this:

NOTE: Invalid argument to function INPUT at line

with the line and column referencing where your INPUT function call was encountered.

Did you read the log? I suspect that if all your dates are later than the year 2012 that every record generated an invalid data message until your limit setting was reached. Which would explain why the values were missing.

 

Kurt_Bremser
Super User

First of all, you don't have a date value. You have a number which looks like a date-related value, but is not in reality.

So you first must convert the number to a string, which can then be used as input for a function using a date informat.

Sunce you already have a numeric variable, you can convert "in place":

numvar = input(put(numvar,z6.),yymmn6.);
format numvar yymmd7.;
yabwon
Onyx | Level 15

Try this:

data number2date;
  n = 201603 ;

  format d date11.;
  d = MDY(mod(n,100),1,n/100);
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 14499 views
  • 1 like
  • 5 in conversation