BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
abdulla
Pyrite | Level 9
set A   Set B    Result     
IDYrincome yrpr1pr2pr3 IDYrincomepr1pr2pr3
10120112122 20110.540.250.36 101201121220.540.250.36
10120121000 20120.470.950.147 101201210000.470.950.147
10120132000 20130.2130.2540.369 101201320000.2130.2540.369
10220111450      102201114500.540.250.36
10220129000      102201290000.470.950.147
10220133250      102201332500.2130.2540.369
10320114500      103201145000.540.250.36
10320123254      103201232540.470.950.147
10320132987      103201329870.2130.2540.369
10420111250      104201112500.540.250.36
104201245069      1042012450690.470.950.147
10420132587      104201325870.2130.2540.369
10520113698      105201136980.540.250.36
10520129875      105201298750.470.950.147
105201310000      1052013100000.2130.2540.369
106201125893      1062011258930.540.250.36
10620122005      106201220050.470.950.147
106201324793      1062013247930.213

.254

0.369

 

 

Hi, I  want the results as given in result section. After we merge, I want to populate the data from set B in my result. I have to merge data by yr. Please help

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@abdulla wrote:

I think in my file the date is not formatted the same way as in other. in one file date is Best12 and informat is 12 (this is the sas format) format and in the other file Date is formatted as YYMMDDN8. informat YYMMDD6. Could you please show me how I can format date as Best12 and infomat 12


So one dataset has just numbers and the other as actual date values. 

You should convert the one where the dates were read in wrong into actual date values.  Then you can work with them as dates.

I recommend attaching the YYMMDD10. format instead of YYMMDDN8. format so that when you print the values humans will recognize them as dates.

data fixed_A ;
  set A;
  datevar= input(put(datevar,8.),yymmdd8.);
  format datevar yymmdd10.;
run;

View solution in original post

10 REPLIES 10
Reeza
Super User
What have you tried so far?
It seems like a straightforward merge to me.

Check the Match Merge section on this page for detailed examples including code:
https://documentation.sas.com/?docsetId=lrcon&docsetTarget=n1tgk0uanvisvon1r26lc036k0w7.htm&docsetVe...
abdulla
Pyrite | Level 9
I checked there. But I don't see how it works for my example. My code was
Data want;
merge setA SetB;
By yr;
run;
It merges but doesn't populate numbers from set B in all the cells. Some cell remain empty
PaigeMiller
Diamond | Level 26

@abdulla wrote:
I checked there. But I don't see how it works for my example. My code was
Data want;
merge setA SetB;
By yr;
run;
It merges but doesn't populate numbers from set B in all the cells. Some cell remain empty

Did you sort setA and setB by year? (Actually setB doesn't need to be sorted, it already is)

 

Can you show us the incorrect data set that got created?

--
Paige Miller
abdulla
Pyrite | Level 9

I think in my file the date is not formatted the same way as in other. in one file date is Best12 and informat is 12 (this is the sas format) format and in the other file Date is formatted as YYMMDDN8. informat YYMMDD6. Could you please show me how I can format date as Best12 and infomat 12

Reeza
Super User

@abdulla wrote:
I think in my file the date is not formatted the same way as in other. in one file date is Best12 and informat is 12 (this is the sas format) format and in the other file Date is formatted as YYMMDDN8. informat YYMMDD6.

You've shown data with only years not dates of any kind. A formatted variable of YYMMDDN8 would not look like the example you posted:

 

ID Yr income   yr pr1 pr2 pr3   ID Yr income pr1 pr2 pr3
101 2011 2122   2011 0.54 0.25 0.36   101 2011 2122 0.54 0.25 0.36
101 2012 1000   2012 0.47 0.95 0.147   101 2012 1000 0.47 0.95 0.147
101 2013 2000   2013 0.213 0.254 0.369   101 2013 2000 0.213 0.254 0.369

 

 

Here are instructions on how to provide sample data as a data step:
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

To solve this issue you can convert each variable first to the same type, numeric as a year, a SAS date with a year format or a character variable with the year and then merge them. 

You could also do the merge via SQL and do the conversion in the JOIN step. 

 

 

PaigeMiller
Diamond | Level 26

Formatting should have no impact on merging. Show us the code and the log and your data.

--
Paige Miller
Tom
Super User Tom
Super User

@abdulla wrote:

I think in my file the date is not formatted the same way as in other. in one file date is Best12 and informat is 12 (this is the sas format) format and in the other file Date is formatted as YYMMDDN8. informat YYMMDD6. Could you please show me how I can format date as Best12 and infomat 12


So one dataset has just numbers and the other as actual date values. 

You should convert the one where the dates were read in wrong into actual date values.  Then you can work with them as dates.

I recommend attaching the YYMMDD10. format instead of YYMMDDN8. format so that when you print the values humans will recognize them as dates.

data fixed_A ;
  set A;
  datevar= input(put(datevar,8.),yymmdd8.);
  format datevar yymmdd10.;
run;
abdulla
Pyrite | Level 9
Thank you very much Tom.
Reeza
Super User

Post the code you ran and exact log please. 

 


@abdulla wrote:
I checked there. But I don't see how it works for my example. My code was
Data want;
merge setA SetB;
By yr;
run;
It merges but doesn't populate numbers from set B in all the cells. Some cell remain empty

 

Tom
Super User Tom
Super User

Seems straight forward.  Note make sure that the "extra" variables in B, PR1-PR3, do not already exist in A.

data a ;
  input ID Yr income  ;
cards;
101 2011 2122  
101 2012 1000  
101 2013 2000  
102 2011 1450  
102 2012 9000  
102 2013 3250  
103 2011 4500  
;

data b;
  input yr pr1 pr2 pr3 ;
cards;
2011 0.54 0.25 0.36
2012 0.47 0.95 0.147
2013 0.213 0.254 0.369
;

proc sort data=a;
  by yr id ;
run;

data want;
  merge a b ;
  by yr;
run;

proc print;
run;

Obs     ID     Yr     income     pr1      pr2      pr3

 1     101    2011     2122     0.540    0.250    0.360
 2     102    2011     1450     0.540    0.250    0.360
 3     103    2011     4500     0.540    0.250    0.360
 4     101    2012     1000     0.470    0.950    0.147
 5     102    2012     9000     0.470    0.950    0.147
 6     101    2013     2000     0.213    0.254    0.369
 7     102    2013     3250     0.213    0.254    0.369

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 16. 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
  • 10 replies
  • 972 views
  • 0 likes
  • 4 in conversation