BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
CHL0320
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
biopharma
Quartz | Level 8

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 ;

View solution in original post

5 REPLIES 5
CHL0320
Obsidian | Level 7
9012010 means Sep. 01 2010
Tom
Super User Tom
Super User

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

 

biopharma
Quartz | Level 8

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 ;
Kurt_Bremser
Super User

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
Tom
Super User Tom
Super User
Note you could read both sets of records using the same input statement.
input id $ date :mmddyy. ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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