BookmarkSubscribeRSS Feed
tianerhu
Pyrite | Level 9
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

tianerhu_0-1623724231436.png

where are the Day1 and Day2?

4 REPLIES 4
japelin
Rhodochrosite | Level 12

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;

 

jimbarbour
Meteorite | Level 14

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).

jimbarbour_0-1623734678751.png

 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;

jimbarbour_1-1623734899395.png

 

Jim

andreas_lds
Jade | Level 19

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
;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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