BookmarkSubscribeRSS Feed
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I would like to join conditionally the data : if ctant.ctant_pere is doesn't missing I'm using this key, in other way I'm using ctant.contrac.

But my code is doesn't work, could you tell me where is the problem, please?

proc sql;
	create table date_naiss
		as select 
			pers.D_NAISS, sousc.no_pol, coalesce(ctant.ctant_pere,ctant.contrac) as COAL
		from dossier sousc, 
			dossier2 ctant, personne pers where 
			and sousc.contr = calculated COAL
			and ctant.pers = pers.pers;
quit;

Thank you very much!

4 REPLIES 4
maguiremq
SAS Super FREQ

Are you getting an error?

 

Code is untested, but you can just move that COALESCE to the join.

 

PROC SQL;
CREATE TABLE date_naiss AS
SELECT
  pers.date_naiss
  , sousc.no_pol
  , COALESCE(ctant.ctant_pere, ctant.contrac) AS coal
FROM
  dossier sousc
  , dossier2 ctant
  , personne pers
WHERE
 sousc.countr = COALESCE(ctant.pere, ctant.contrac)
 AND ctant.pers = pers.pers
;
QUIT;

Again, I think that works but I can't tell without data.

 

I'd assume the calculated doesn't work in the join.

SASdevAnneMarie
Barite | Level 11
Thank you, I already tried it, but seems thant COALESCE statement doesn't work with join.
ballardw
Super User

Doesn't work is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "</>" icon or attached as text to show exactly what you have and that we can test code against.

Tom
Super User Tom
Super User

Are you sure the issue is the COALESCE() function call?  It seems to work fine for me

570  proc sql;
571  create table test as select a.*,coalesce(a.height,a.weight) as NEWVAR
572  from sashelp.class a, sashelp.class b
573  where b.height=calculated newvar;
NOTE: Table WORK.TEST created, with 23 rows and 6 columns.

Your WHERE clause looks invalid since it has the binary operator AND with only one input. Either remove the AND or place some other binary expression before it.

proc sql;
create table date_naiss as 
select pers.D_NAISS
     , sousc.no_pol
     , coalesce(ctant.ctant_pere,ctant.contrac) as COAL
from dossier sousc
   , dossier2 ctant
   , personne pers 
where (1=1) 
  and sousc.contr = calculated COAL
  and ctant.pers = pers.pers
;
quit;

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 708 views
  • 0 likes
  • 4 in conversation