DATA Step, Macro, Functions and more

How can I split a table and join

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

How can I split a table and join

I have two tables on is

id  value

1     aa

2     bb

3     cc

the other is

No    value

25      aa

25      bb

25       cc

26       aa

26      bb

26      cc

I need a output like this

id  value     no   value    no   value

1     aa        25      aa       26     aa

2     bb         25      bb       26     bb

3     cc         25       cc        26     cc

 

Thanks.

 


Accepted Solutions
Solution
‎07-27-2017 01:05 PM
Trusted Advisor
Posts: 1,137

Re: How can I split a table and join

Please try the below code, you will get the expected output. however the variable names will be different. So if you want to display the variable names same then please update the label statement as mentioned in the code.

 

 

data have1;
input id  value$;
cards;
1     aa
2     bb
3     cc
;
data have2;
input No    value$;
cards;
25      aa
25      bb
25       cc
26       aa
26      bb
26      cc
;

proc sort data=have1;
by value;
run;

proc sort data=have2;
by value;
run;

data have;
merge have1 have2;
by value;
run;

data have;
set have;
by value;
retain num;
if first.value then num=1;
else num+1;
run;

data want;
set have;
array val(*) $ value1-value2;
array nos(*)$ no1-no2;
by value notsorted;
retain value1-value2 no1-no2;
if first.value then do;
call missing (of val(*));
call missing (of nos(*));
end;
val(num)=value;
nos(num)=no;
if last.value;
drop num;
label value1='Value'
      value2='Value'
      no1='No'
      no2='No'; run;

image.png

Thanks,
Jag

View solution in original post


All Replies
Super User
Posts: 11,343

Re: How can I split a table and join

If you want a data set as output you should provide some additional variable names as you can only have one variable named "value" in a data set.

Or do you want a report with text?

Super User
Posts: 5,441

Re: How can I split a table and join

To me your output looms like a report. If that's your goal I suggest that you simply first join on the columns "value", and then use e.g. PROC REPORT to get the desired layout.
Data never sleeps
Super User
Posts: 19,876

Re: How can I split a table and join

Do you need a dataset or a report that looks like that? 

 

Solution
‎07-27-2017 01:05 PM
Trusted Advisor
Posts: 1,137

Re: How can I split a table and join

Please try the below code, you will get the expected output. however the variable names will be different. So if you want to display the variable names same then please update the label statement as mentioned in the code.

 

 

data have1;
input id  value$;
cards;
1     aa
2     bb
3     cc
;
data have2;
input No    value$;
cards;
25      aa
25      bb
25       cc
26       aa
26      bb
26      cc
;

proc sort data=have1;
by value;
run;

proc sort data=have2;
by value;
run;

data have;
merge have1 have2;
by value;
run;

data have;
set have;
by value;
retain num;
if first.value then num=1;
else num+1;
run;

data want;
set have;
array val(*) $ value1-value2;
array nos(*)$ no1-no2;
by value notsorted;
retain value1-value2 no1-no2;
if first.value then do;
call missing (of val(*));
call missing (of nos(*));
end;
val(num)=value;
nos(num)=no;
if last.value;
drop num;
label value1='Value'
      value2='Value'
      no1='No'
      no2='No'; run;

image.png

Thanks,
Jag
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 150 views
  • 1 like
  • 5 in conversation