BookmarkSubscribeRSS Feed
ecam
Fluorite | Level 6
I have a couple. sets of dates
201802
20180201

They both read as numeric.
Now I need to be able to flip between the two formats

I’ve read about combining input() and put to flip. But it’s already numeric. So not sure how this works.

Thanks in advance for the assist
8 REPLIES 8
ecam
Fluorite | Level 6
First of u believe the formats are

Yymmddn8
Yymmddn6

But I can’t get them to behave when I try to convert them between formats
Tom
Super User Tom
Super User

@ecam wrote:
First of u believe the formats are

Yymmddn8
Yymmddn6

But I can’t get them to behave when I try to convert them between formats

If we try to interpret the '201802' value in your original post as representing a date displayed using the YYMMDDN6 format then that would mean that day of month as 2, month of year as 18 and year in century as 20.  Since there are only 12 months in a year that is impossible.  More likely that string should be interpreted as meaning the second month in the year 2018.  The day of the month is unknown.

 

Kurt_Bremser
Super User

Show us the as-is state by posting example data in usable form (data step with datalines).

Then show what you want to get as end result from that. Is it a new dataset, or a report? Note that you can only have one format per column throughout a dataset.

ecam
Fluorite | Level 6
The information I Have was in my first post

The dates are coming in as numeric as follows.
201802.
20180201

Because the macro files are so old I don’t want to reinvent the wheel. So depending on the dataset and task. I would be staring at two dataset with the two different date formats that I need to combine.

When I try to use input () and put () to convert I get crazy numbers and if I try to reformat using yymmdd10. I get 02/01/2018 for the last example and garbage for the 201802.

I need to be able to convert either format into the other since I get change the code to standardize.

I hope this helps.

My goal is a new dataset with one format ideally the 20180201 format
Kurt_Bremser
Super User

I CAN read, and what you gave me to read was not enough to understand and solve the issue. That should be very obvious from my answer.

 

POST.EXAMPLE.DATA.IN.DATASTEPS.WITH.DATALINES. That allows to recreate the dataset AS IT REALLY IS. No ambiguities about content and attributes like types, formats, lengths.

 

Help us to help you, it is NOT rocket science.

novinosrin
Tourmaline | Level 20

Hi @ecam  , @Kurt_Bremser  , I and most others need

 

what you

1. HAVE (sample input)

2. What you WANT( sample output for your input i.e expected)

3. A brief explanation of the business/convert logic

 

All above in simple terms. We request you to post the above in the easiest description/language possible that is perhaps comprehensive to even non native English speakers like me. 

 

Thank you & Have a nice day!

ballardw
Super User

@ecam wrote:
The information I Have was in my first post

The dates are coming in as numeric as follows.
201802.
20180201

Because the macro files are so old I don’t want to reinvent the wheel. So depending on the dataset and task. I would be staring at two dataset with the two different date formats that I need to combine.

When I try to use input () and put () to convert I get crazy numbers and if I try to reformat using yymmdd10. I get 02/01/2018 for the last example and garbage for the 201802.

I need to be able to convert either format into the other since I get change the code to standardize.

I hope this helps.

My goal is a new dataset with one format ideally the 20180201 format

Are these "dates" in a data set? If so run Proc Contents on the data set and show us result. I strongly suspect that you do not have date values but simple numeric values that are attempting to treat as dates. The Proc contents will tell us exactly what sort of data you are dealing with.

 

FORMATS have no affect on the ability to combine data sets. Example:

data work.data1;
   x='01JAN2019'd;
   format x date9.;
run;

data work.data2;
   x='02JAN2019'd;
   format x mmddyy10.;
run;

data work.combined;
   set work.data1
       work.data2
   ;
run;

However the first data set containing a common variable when using SET or MERGE will determine the default format.

 

And without the code you actually use to do the input or conversion plus the actual inputs it is impossible to diagnose what might cause the "garbage". One suspects use of the incorrect informat and possibly an incorrect put to get the string for the informat to read.

 

Also a FORMAT is a variable property. Within a data set, a variable only can have one format. If you want to see different values then would need to create a character variable applying the appropriate format as desired.

Tom
Super User Tom
Super User

@ecam wrote:
I have a couple. sets of dates
201802
20180201

They both read as numeric.
Now I need to be able to flip between the two formats

I’ve read about combining input() and put to flip. But it’s already numeric. So not sure how this works.

Thanks in advance for the assist

How do you have these sets?  Are they in actual SAS datasets?    Are they in text files? Post examples of each type of "set of dates".

When you say "read as numeric" what does that mean? Do you mean that when the original raw text was input as the numbers 201,802 and 20,180,201?

 

You later mention macro.  Does this mean you have macro variables with these values in them?  Note that macro variables always contain text. 

 

What dates do you want those two strings of digits represent?

 

Let's assume you have a macro variable named DATE that can have either 6 digits or 8 digits and you want to interpret them as YYYYMM or YYYYMMDD form and create an actual date.  In that case you just need to append 01 and read the first 8 digits.

%let date1=201802;
%let date2=%sysfunc(inputn(&date1.01,yymmdd8));

Similarly in a data step

date1=201802;
date2=input(cats(date1,'01'),yymmdd8.);

When DATE1 is already 8 digits long the extra 01 that is appended will be ignored.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 8 replies
  • 825 views
  • 0 likes
  • 5 in conversation