First, I create a data set as following:
libname apple 'C:\Users\liaodong\Documents\My SAS Files\prep guide\base
\70 question\question 57';
data apple.one;
length BeginDate $ 9.;
input BeginDate $ ;
datalines;
09JAN2010
12JAN2010
;
proc print data = apple.one;
run;
then, I input data set above:
libname apple 'C:\Users\liaodong\Documents\My SAS Files\prep guide\base
\70 question\question 57';
data apple.two;
set apple.one;
Day1 = weekday(BeginDate);
Day2 = day(BeginDate);
format BeginDate date9.;
run;
proc print data = apple.two;
run;
I think that there should be three variable , BeginDate , Day1 and Day2 ?
But the result is not
where are the Day1 and Day2?
Is there an error in the log when creating the data set "apple.two"?
The error suggests that the dataset was not created correctly, so the proc print step prints the dataset "apple.one" that was created successfully just before.
As for the error, I think it is because BeginDate is a character variable, so the date9 format cannot be applied.
If you use the input function to convert it to a date value and then process it as shown below, you may get the desired result.
data apple.two;
set apple.one;
Day1 = weekday(input(BeginDate,date9.));
Day2 = day(input(BeginDate,date9.));
run;
If you look closely in your SAS log, there will be errors listed. One issue is that BeginDate cannot be character. Date functions like DAY() and WEEKDAY() do not work on character data. They only work on numeric data. Dates in SAS are store as numbers (the count of days starting on Jan 1st, 1960).
You need to first read in the data as character and then transform it into a true SAS value using the INPUT() function. The INPUT() function converts characters into numbers based on the INFORMAT specified. Here, I am using ANYDTDTE which will read in almost any date or date-time type variable and extract the date. Once you have transformed the data, then you can use DAY() and WEEKDAY().
I've adjusted the code as follows, below. Below that are the results.
data one;
DROP _:;
length BeginDate 8;
length _Temp_Date $9;
input _Temp_Date $ ;
BeginDate = INPUT(_Temp_Date, ANYDTDTE9.);
datalines;
09JAN2010
12JAN2010
;
proc print data = one;
run;
data two;
set one;
Day1 = weekday(BeginDate);
Day2 = day(BeginDate);
format BeginDate date9.;
run;
proc print data = two;
run;
Jim
Read the log, always! Especially if the result of a program does not meet your expectations.
The problem (BeginDate is a char-variable) is reported in the second step, but caused by not properly reading the variable in the first data step. So i would fix the problem there:
data work.one;
length BeginDate 8; /* <- changed to numeric */
/* informat so that the variable BeginDate */
format BeginDate date9.;
informat BeginDate date9.;
input BeginDate;
datalines;
09JAN2010
12JAN2010
;
Maxim 2
Read the Log.
Read the Log.
Read the Log.
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.
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.
Ready to level-up your skills? Choose your own adventure.