BookmarkSubscribeRSS Feed
Walternate
Obsidian | Level 7

Hi,

I am creating a date variable in my dataset out of two other date variables, DateVar1 and DateVar2. If the person has a value for DateVar1, the new date var will take that value; if they do not, the new date var will take the value for DateVar2, like this:

data want;

format new_date_var date9.;

set have;

if DateVar1 ne . then NewDateVar=DateVar1;

else if DateVar1=. then NewDateVar=DateVar2;

run;

Now I'm trying to pull out the dates for which my categorical variable, CategVar, has a value of A, so I can see which dates fall into that category. I tried this:

proc sql;

create table which_dates as

select new_date_var from want

where CategVar='A';

quit;

It runs fine, but the date variable is just filled with asterisks rather than values, even when I insert a formatting statement:

proc sql;

create table which_dates as

select new_date_var format date9. from want

where CategVar='A';

quit;

Any help is much appreciated.

4 REPLIES 4
Reeza
Super User

What does your log say?

It looks like you're missing the = sign in the format statement

select new_date_var format=date9.

Walternate
Obsidian | Level 7

My log just says that the which_dates table was created.

NOTE: Table WORK.which_dates created, with 342 rows and 1 columns.

19         quit;

NOTE: PROCEDURE SQL used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

I tried the = and that didn't make a difference.

dcruik
Lapis Lazuli | Level 10

What do the values of the NewDateVar look like after your DATA step?  Also, I noticed that your NewDateVar has underscores in it in the SQL procedure and not in the DATA step (not sure if it's that way, or just in your example you gave).

Tom
Super User Tom
Super User

Probably because you have applied a DATE format to a DATETIME value.  When the year gets above 4 digits it is hard for the DATE9 format to work.

data _null_;

  date=date();

  datetime=datetime();

  put date= datetime= ;

  put (date datetime) (= date9.);

  put (date datetime) (= dtdate9.);

run;

date=20265 datetime=1750943816.2

date=26JUN2015 datetime=*********

date=01JAN1960 datetime=26JUN2015

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 7108 views
  • 1 like
  • 4 in conversation