BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Annie_Fréchette
Obsidian | Level 7

Hi! I'm having some issues with a variable. Test_date is , when I double click ont it, a character type. But it's a date. Ex:18-12-2017. I try to convert it in a date format (DDMMYY10.;) but it doesn't work.

data work.f2sd;
set work.f2sd;
informat Test_date DDMMYY10.;
format Test_date DDMMYY10.;
run;

Here is my log:

326 data work.f2sd;
327 set work.f2sd;
328 informat Test_date $DDMMYY10.;
----------
48
ERROR 48-59: Le(la) informat $DDMMYY est introuvable ou n'a pas pu être chargé(e).

329 format Test_date $DDMMYY10.;
----------
48
ERROR 48-59: Le(la) format $DDMMYY est introuvable ou n'a pas pu être chargé(e).

330 run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.F2SD may be incomplete. When this step was stopped there were 0
observations and 8 variables.
WARNING: Table WORK.F2SD non remplacée car cette étape a été interrompue.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds

 

 

Anyone can help?

And I have a second question: How could I code for this steps:

First, class the dates from older to newer;

if date is before 31-01-2018, then delete this line.

 

Thanks a lot!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

An INFORMAT is used when converting text to values. Attaching it to an existing variable does not really do anything if you are not reading values into that variable.  And as the error message says your variable is already a character variable so you cannot attach the numeric informat to it.

 

Looks like you need to create a new numeric variable from your existing character variable.  Here is code to create a new numeric variable named ACTUAL_DATE from your existing character variable TEST_DATE.

data work.f2sd_fixed;
  set work.f2sd;
  actual_date = input(Test_date,DDMMYY10.);
  format actual_date DDMMYY10.;
run;

Notice how I used a new name for the dataset also.  If you use the same name then the original dataset will be deleted when the new dataset is created.

I did not bother to attach an informat to the new variable since it is of not much use for an existing variable.  But I did attach a format so the number of days that are stored in ACTUAL_DATE will be displayed in way that a human could understand.  (Personally avoid using DMY or MDY order for dates to avoid people confusing the tenth of December and November twelfth. I always use either DATE or YYMMDD format for dates.)

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

An INFORMAT is used when converting text to values. Attaching it to an existing variable does not really do anything if you are not reading values into that variable.  And as the error message says your variable is already a character variable so you cannot attach the numeric informat to it.

 

Looks like you need to create a new numeric variable from your existing character variable.  Here is code to create a new numeric variable named ACTUAL_DATE from your existing character variable TEST_DATE.

data work.f2sd_fixed;
  set work.f2sd;
  actual_date = input(Test_date,DDMMYY10.);
  format actual_date DDMMYY10.;
run;

Notice how I used a new name for the dataset also.  If you use the same name then the original dataset will be deleted when the new dataset is created.

I did not bother to attach an informat to the new variable since it is of not much use for an existing variable.  But I did attach a format so the number of days that are stored in ACTUAL_DATE will be displayed in way that a human could understand.  (Personally avoid using DMY or MDY order for dates to avoid people confusing the tenth of December and November twelfth. I always use either DATE or YYMMDD format for dates.)

Annie_Fréchette
Obsidian | Level 7

Thanks, it has worked!

And how could I ask SAS for this:

 

First, class the dates from older to newer;

if date is before 31-01-2018, then delete this line.

if date is after 31-01-2019 then delete this line.

 

Thanks!

Annie_Fréchette
Obsidian | Level 7

Thanks! It worked!

Tom
Super User Tom
Super User

@Annie_Fréchette wrote:

Thanks, it has worked!

And how could I ask SAS for this:

 

First, class the dates from older to newer;

if date is before 31-01-2018, then delete this line.

if date is after 31-01-2019 then delete this line.

 

Thanks!


Not sure what you mean by class.  You can use PROC SORT to order the observations by the value of date.  But many procedures will group, or class, the data without having to first order the data.

 

To subset the data you can use a WHERE statement (or in a data step a subsetting IF or IF/THEN/DELETE).  To reference a specific date in code you use a quoted string in a style the DATE informat understands suffixed with the letter d.

where actual_date between '31jan2018'd and '31jan2019'd;
Annie_Fréchette
Obsidian | Level 7

Thanks Tom, I just learned two ways of getting my specified dates!:)

Kurt_Bremser
Super User

SAS dates are numeric. Since you cannot change the type of a variable, you need to replace it with a newly created variable:

data work.f2sd (drop=_td);
set work.f2sd (rename=(test_date=_td));
Test_date = input(_td,ddmmyy10.);
format Test_date DDMMYY10.;
run;

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!
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
  • 7 replies
  • 2102 views
  • 2 likes
  • 3 in conversation