BookmarkSubscribeRSS Feed
withpeople86
Calcite | Level 5

I want to make a table by combing the following two tables. 

dydm age_groupcounts
19971220
19971330
19971420
1997225
19972314
19972423
19973123
19973222
19973311
1997345
19974152
19974234
19974325
19974423
19981153
1998125
19981332
19981420
19982144
19982242
19982311
1998245
19983134
19983220
19983330
19983432
19984110
19984223
19984310
19984420

table a

 

yearage_grouptotalstd
19971350028
19972650025
19973760017
199742430030
19981360028
19982650025
19983780017
199842430030

table B

 

What I want looks like this. 

ddateage_groupcountstotalstd
1997/0110350028
1997/01220650025
1997/01330760017
1997/014202430030
1997/0210350028
1997/0225650025
1997/02314760017
1997/024232430030
1997/03123350028
1997/03222650025
1997/03311760017
1997/03452430030
1997/04152350028
1997/04234650025
1997/04325760017
1997/044232430030
1998/01153360028
1998/0125650025
1998/01332780017
1998/014202430030
1998/02144360028
1998/02242650025
1998/02311780017
1998/02452430030
1998/03134360028
1998/03220650025
1998/03330780017
1998/034322430030
1998/04110360028
1998/04223650025
1998/04310780017
1998/044202430030

 

should I change the year and month variable to the calendar format?

How can I join these tables with proc sql?

Thanks for taking the time to look at it in advance.

 

 

 

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

Post your data as a data step.

This will allow us to know for example if you have SAS dates or strings.

 

heffo
Pyrite | Level 9

I couldn't really get the missing values in there. It shouldn't be any problems but I guess I'm tired. So, I added a step in the middle to add the missing age_groups. 

data TableA;
input dy	dm 	age_group	counts;
datalines;
1997 1 2 20
1997 1 3 30
1997 1 4 20
1997 2 2 5
1997 2 3 14
1997 2 4 23
1997 3 1 23
1997 3 2 22
1997 3 3 11
1997 3 4 5
1997 4 1 52
1997 4 2 34
1997 4 3 25
1997 4 4 23
1998 1 1 53
1998 1 2 5
1998 1 3 32
1998 1 4 20
1998 2 1 44
1998 2 2 42
1998 2 3 11
1998 2 4 5
1998 3 1 34
1998 3 2 20
1998 3 3 30
1998 3 4 32
1998 4 1 10
1998 4 2 23
1998 4 3 10
1998 4 4 20
;;;
run;
 
data tableB;
input year	age_group	total	std;
datalines;
1997 1 3500 28
1997 2 6500 25
1997 3 7600 17
1997 4 24300 30
1998 1 3600 28
1998 2 6500 25
1998 3 7800 17
1998 4 24300 30
;;;
run;
data tableb;
	set tableb;
	do _i = 1 to 4;
		dm =_i;
		output;
	end;
	drop _i;
run;

proc sort data=tablea (rename=(dy=year));
	by year age_group dm;
run;
proc sort data=tableb;
	by year age_group dm;
run;
data want;
	retain ddate	age_group	counts	total	std;
	length ddate $ 10;
	update tableb tablea  ;
	by year age_group dm;
	ddate =cat(year,"/",dm);
	if counts = . then counts = 0;
	drop year dm;
run;
proc sort data=want;
	by ddate age_group;
run;

Stupid but it works? 🙂

 

 

PGStats
Opal | Level 21

Do something like:

 

proc sql;
create table C as
select 
    mdy(dm,1,dy) as ddate format=yymms.,
    a.age_group,
    counts,
    total,
    std
from A inner join B on dy=year and A.age_group=B.age_group
order by ddate, age_group;
quit;
PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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