BookmarkSubscribeRSS Feed
ssamuel
Calcite | Level 5

No comando abaixo estou juntando as tabelas TA com a TB gerando a TAmaisTB (TA e TB tem as mesmas colunas):

data WORK.TAmaisTB;
set WORK.TA
WORK.TB;
run;

O comando roda sem problemas, desde que ambas as tabelas tenham dados.

Caso a primeira  delas não tenha dados eu recebo o seguinte erro:

ERROR: Column 2 from the first contributor of OUTER UNION is not the same type as its counterpart from the second.
ERROR: Column 3 from the first contributor of OUTER UNION is not the same type as its counterpart from the second.

 

Qual a melhor forma para tratar este erro?

1 REPLY 1
SASJedi
SAS Super FREQ

Acho que o código citada acima não pode ser o código que produziu o erro reportado. O erro parece que foi gerado pelo SQL, usando um operador UNION. Olha só:

/* Crie as tabelas de origem */
data ta;
	infile datalines dsd dlm='|';
	input Coluna1:$2. Coluna2-coluna4;
datalines;
A|1|2|3
B|2|3|4
C|3|4|5
;
data tb;
	infile datalines dsd dlm='|';
	input Coluna1-coluna4;
datalines;
1|1|2|3
2|2|3|4
3|3|4|5
;

/*Tentando juntar as tabelas usando o DATA step*/
data WORK.TAmaisTB;
	set WORK.TA
	WORK.TB;
run;

E no Log é mostrada esta mensagem de erro:

 

 

ERROR: Variable Coluna1 has been defined as both character and numeric.
99   run;

NOTE: The SAS System stopped processing this step because of errors.

Mas se tento juntar as tabelas usando SQL:

 

/*Tentando juntar as tabelas usando SQL*/
proc sql;
create table TAmaisTB as
select * from ta
	union all
select * from tb
;
quit;

A mensagem no log é semelhante à mensagem de erro relatada:

ERROR: Column 1 from the first contributor of UNION ALL is not the same type as its counterpart from
       the second.

Em qualquer caso, o problema é causado pelo fato de uma das colunas na tabela TA tem o mesmo nome que uma das colunas da tabela TB, mas o tipo de dados das colunas não correspondem. Se converte o tipo de dado de uma das colunas, vai fazer certo. Talvez pareça mais fácil usando SQL:

 

proc sql;
create table TAmaisTB as
select * from ta
	union all
select strip(put(Coluna1,32.)) as Coluna1
		,coluna2
		,coluna3
	from tb
;
quit;

Mas o processo produzirá um AVISO no Log:

 

 

WARNING: A table has been extended with null columns to perform the UNION ALL set operation.
NOTE: Table WORK.TAMAISTB created, with 6 rows and 4 columns.

O código do DATA step é menor:

 

 

data WORK.TAmaisTB;
	set WORK.TA 
	    WORK.TB (rename=(coluna1=x));
	if not missing(x) then coluna1=strip(put(x,32.));
	drop x;
run;

E produz nenhuma mensagem de ERROR ou WARNING no Log:

 

 

NOTE: There were 3 observations read from the data set WORK.TA.
NOTE: There were 3 observations read from the data set WORK.TB.
NOTE: The data set WORK.TAMAISTB has 6 observations and 4 variables.

Boa sorte!
Mark

 

 

 

 

Check out my Jedi SAS Tricks for SAS Users