BookmarkSubscribeRSS Feed
Cooksam13
Fluorite | Level 6

Hi

I am trying to have the date stay the same in both of these data sets and then remain the same after they combine 

 

I do this step and they both show just a year for the date which is what I want

 

data work.vchange;
set new.vital;
if hepb_date ne '?' then do;
   hbv2 = input(hepb_date, YYMMDD10.);
   end;
   format bcdob Dt_birth hbv2 yymmdd10.;
   dtbirth=dt_birth; cdob=bcdob;
	drop Dt_Birth BCDOB hepb_date; 
   format bcdob dtbirth cdob Dt_birth hbv2 year.;

  run;

and then I get this result :

Cooksam13_0-1651276990412.png

I then do somethin similar in the fo the other piece of data

data work.nchange;
   set new.ncac;
   Dtbirth = input(Dt_birth, 8.);
   if BCDOB^='NULL' then do;
  CDOB = input(BCDOB, 8.);
   end;
   hbv2 = hepb_date;
   format  hbv2 year.;
   format CDOB Dtbirth 8.;
   drop bcdob dt_birth hepb_date;
run;

and get this 

Cooksam13_1-1651277049857.png

 

 

 

but as soon as I combine them 

they get all messed up and the year doesn't match the set before 

it looks like this:

data work.complete;
set work.nchange work.vchange;
run;

Cooksam13_2-1651277106297.png

some do remain the same but my thought is that the ones that are saved as dates in one data set get changed to numeric values once combined.

How do i get this from not happening and keep the year?

 

1 REPLY 1
Tom
Super User Tom
Super User

It did what you asked it to do.

 

In the first step you are assuming that CDOB has DATE values (number of days since 1960) because you asked SAS to use the YEAR format to display it.

In the second step you are putting INTEGER values into CDOB because you used the 8. informat to create the value and asked SAS to use the 8. format to display it.

 

When you combine two dataset like in your third step and don't use a FORMAT statement then SAS will use the first format that is sees attached to the variable.  Since that is the 8. format from the NCHANGE dataset.  So the date values it reads from VCHANGE will be displayed as integers.  Formats do not change the values that are stored, just how they are displayed.

 

So which type of values do you want CDOD to have? 

Should it have a count of years like the 2,109 values you have in NCHANGE?

Or should it have count of days like the 21,911 values you have in the VCHANGE dataset?

 

It is much harder to convert years into a date (which month? Which day in that month?) so you should convert the date (number of days) into a year (number of years) by using the YEAR() function.  Do not attach the YEAR format to a variable that has number of years, it will cause years between 0 and 364 to print as 1960, years from number 365 to 365+365 appear as 1961 , etc.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 388 views
  • 1 like
  • 2 in conversation