BookmarkSubscribeRSS Feed
Gieorgie
Quartz | Level 8

I would like to change the date format to the picture below 2021-01 eg I found in the documentation that it is YYMMw. However, when he tries to format, he gets this result.

When i tried use subtr like this:

    ,input(substr(t1.PRP_END_DATE,1,4)||'-'||substr(t1.PRP_END_DATE,5,2),yymmdd10.)		as POLICY_VINTAGE,

i got error :ERROR: Function SUBSTR requires a character expression as argument 1.

Gieorgie_0-1635255702740.png

This my result :

Gieorgie_1-1635255745884.png

And this is my code:

PROC SQL;     
create table PolisyEnd as 
    select distinct  
  ,datepart(t1.PRP_END_DATE) as PRP_END_DATE format yymmdd10.
	,datepart(t1.PRP_END_DATE) as POLICY_VINTAGE format YYMMw.,

 

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @Gieorgie,


@Gieorgie wrote:

I would like to change the date format to the picture below 2021-01 (...)

PROC SQL;     
create table PolisyEnd as 
    select distinct  
  ,datepart(t1.PRP_END_DATE) as PRP_END_DATE format yymmdd10.
	,datepart(t1.PRP_END_DATE) as POLICY_VINTAGE format YYMMw.,

Use the YYMMD. format. (The "w" in "YYMMw." is just a place holder for the width specification as in YYMM7..)

PaigeMiller
Diamond | Level 26

@Gieorgie 

We're trying to help you, so please help us. From now on do not show us error messages detached from the code. Please do show us the complete log, so we can see the code and the error message on the next line. Please also format the log properly, by copying the log as text and then pasting it into the window that appears when you click on the </> icon here in the SAS communities.

--
Paige Miller
ballardw
Super User

If you have a date, time, or datetime value then usually the easiest way to get a piece of the value is an appropriate date or time function. If you have a datetime value and want date elements then use the Datepart function to get the date information and then the day, month, year function.

 

data example;
   date = '23Jan2021'd;
   time = "12:45:18"t;
   dt   = "14Jun2021:09:23:14"dt;
   format date date9. time time8. dt datetime18.;
   dayofmonth = day(date);
   monthnumber= month(date);
   yearnumber = year(date);
   hour = hour(time);
   minutes = minute(time);
   seconds = second(time);
   dtday = day(datepart(dt));
   dtmon = month(datepart(dt));
   dthour = hour(dt);
run;

You want to test, as in create a variable with the value of any concatenation involving the || operator. The results may surprise you. Consider this example of concatenating a string with a numeric value 4 different ways. See the different results.

data example2;
   length x $ 30;
   x='abcdefg';
   y=25;
   cat1 = x||y;
   cat2 = cats(x,y);
   cat3 = cat(x,y);
   cat4 = catt(x,y);
run;

Also note in the log that only one of these provides a warning about conversion of numeric to character values.

 

 

 

The MDY function may be something to investigate to create new date value.

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1425 views
  • 0 likes
  • 5 in conversation