Hi,
I would like to create an append dataset with the date variable in mmddyy10 from the source datasets. For example, one of the source dataset (a) has date variable in numeric (9012010 means Sep. 01 2020) and the other source dataset (b) has the date in character. Thank you so much.
data a;
input id $ date;
datalines;
1 9012010
1 10022010
2 9032010
2 10092010
;
run;
data b;
input id $ date $;
datalines;
1 04/15/2017
1 11/09/2016
2 11/10/2016
2 02/01/2017
;
run;
If I understand correctly you have dates represented in different variable types and need to be manipulated to a standard format into one dataset. See if this SQL solution works for you:
proc sql ; create table want as select a.id , input(put(a.date,z8.),mmddyy8.) as date format = mmddyy10. from a outer union corr select b.id , input(b.date,mmddyy10.) as date format = mmddyy10. from b ; quit ;
It is not clear what your expected output is, but let's just show you how to convert both of those to actual date values.
First let's fix the code for B so that it reads the whole date string.
data b;
input id $ date :$10.;
datalines;
1 04/15/2017
1 11/09/2016
2 11/10/2016
2 02/01/2017
;
Now here is how you could convert those two datasets to have a numeric variable named DATE that actual has a DATE value in it.
data fix_a;
set a;
want=input(put(date,z8.),mmddyy8.);
format want yymmdd10.;
rename date=date_numeric want=date;
run;
data fix_b;
set b;
want=input(date,mmddyy10.);
format want yymmdd10.;
rename date=date_string want=date;
run;
Now you can put them together if you want.
data both;
set fix_a fix_b;
run;
proc print;
run;
Results:
date_ date_ Obs id numeric date string 1 1 9012010 2010-09-01 2 1 10022010 2010-10-02 3 2 9032010 2010-09-03 4 2 10092010 2010-10-09 5 1 . 2017-04-15 04/15/2017 6 1 . 2016-11-09 11/09/2016 7 2 . 2016-11-10 11/10/2016 8 2 . 2017-02-01 02/01/2017
If I understand correctly you have dates represented in different variable types and need to be manipulated to a standard format into one dataset. See if this SQL solution works for you:
proc sql ; create table want as select a.id , input(put(a.date,z8.),mmddyy8.) as date format = mmddyy10. from a outer union corr select b.id , input(b.date,mmddyy10.) as date format = mmddyy10. from b ; quit ;
Read the data properky, so you have SAS dates in the first place.
data a;
input id $ temp;
date = input(put(temp,8.),mmddyy8.);
format date yymmddd10.;
drop temp;
datalines;
1 9012010
1 10022010
2 9032010
2 10092010
;
data b;
input id $ date mmddyy10.;
format date yymmddd10.;
datalines;
1 04/15/2017
1 11/09/2016
2 11/10/2016
2 02/01/2017
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!
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.