BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASdevAnneMarie
Barite | Level 11

Hello Experts,

 

I have an anormal join result, for example, the key L004IV is common, but the result of left join is empty. I tried left, compress, right option, but it doesn't work.

My code is :

proc sql;
	create table ACCORD13_1 as select  a.*, b.* 
	from ACCORD2_q as a 
	    left join DONNEEMAQUETTE3_q as b on left(a.CD_PRODUIT)=left(b.CD_PRODUIT)
	;
quit;

The data is joined. 

Thank you very much for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Kurt was right. You has '0D0A'x characters at the end of variable 'CD_PRODUIT' in dataset 'DONNEEMAQUETTE3_q' .

54   data x1;
55    set ACCORD2_q;
56    if compress(CD_PRODUIT,,'kad')='L004IV';
57    put CD_PRODUIT $hex32.;
58   run;

4C30303449562020



59   data x2;
60    set DONNEEMAQUETTE3_q;
61    if compress(CD_PRODUIT,,'kad')='L004IV';
62     put CD_PRODUIT $hex32.;
63   run;

4C30303449560D0A

Therefore, you need to use function COMPRESS to get rid of these characters:

proc sql;
	create table ACCORD13_1 as select  a.*, b.* 
	from ACCORD2_q as a 
	    left join DONNEEMAQUETTE3_q as b on compress(a.CD_PRODUIT,,'kad')=compress(b.CD_PRODUIT,,'kad')
	;
quit;

View solution in original post

16 REPLIES 16
yabwon
Amethyst | Level 16

1) what does the LOG says?

2) run proc contents on ACCORD2_q and DONNEEMAQUETTE3_q and share what you see.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Amethyst | Level 16

OK, I think I got it.

1) Run:

options validvarname=any;

before you run that SQL.

 

2) naming a variable "Frais d'ahésion à l'assos" in SAS is rather silly idea...

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SASdevAnneMarie
Barite | Level 11

Hello,

 

Thank you for your message. I did this, but I still have an empty value for key L004IV :


data ACCORD2_q;
	set ACCORD2;
	rename 'Frais d''ahésion à l''assos'n=Frais_dadhesion;
	keep cd_produit 'Frais d''ahésion à l''assos'n;
run;

data  DONNEEMAQUETTE3_q;
	set DONNEEMAQUETTE3;
	keep  cd_produit Nbcar_arb Nbcar_cngmnt;
run;

options validvarname=any;

/***BASE FINALE***/
proc sql;
	create table ACCORD13_1 as select  a.*, b.* 
		from ACCORD2_q as a 
			left join DONNEEMAQUETTE3_q as b on left(a.CD_PRODUIT)=left(b.CD_PRODUIT)
	;
quit;

SASdevAnneMarie_0-1754071832680.png

 

Kurt_Bremser
Super User

Maxim 3: Know Your Data.

 

Whenever you have such issues, look what's really in there.

First, I filtered your datasets for the problematic key and found that it's mssing in donnemarquette.

Then, I looked at the hex codes:

data check_b;
set DONNEEMAQUETTE3_q;
checkvar = put(cd_produit,$hex16.);
run;

and, lo and behold, in the failing keys there's a hidden 0D0A in the last two characters.

 

SASdevAnneMarie
Barite | Level 11
Thank you Kurt! Did you use the notepad to see the hidden 0D0A?
Kurt_Bremser
Super User

@SASdevAnneMarie wrote:
Thank you Kurt! Did you use the notepad to see the hidden 0D0A?

No. Just look at the check dataset in SAS, it contains a variable where the hex codes are displayed.

Alternatively, you can assign a $HEX format to the variable in question.

SASdevAnneMarie
Barite | Level 11
I don't know, I received this input file.
Kurt_Bremser
Super User

@SASdevAnneMarie wrote:
I don't know, I received this input file.

It didn't fall right out of the sky, now did it?

Somebody sent it to you, and that person must be notified that their process is broken. If you intend to be a professional, you can't let this go on.

If you can't even trust their key values, why trust anything else?

SASdevAnneMarie
Barite | Level 11
The person copy and past values manually so it can’t be improved.
Ksharp
Super User

Kurt was right. You has '0D0A'x characters at the end of variable 'CD_PRODUIT' in dataset 'DONNEEMAQUETTE3_q' .

54   data x1;
55    set ACCORD2_q;
56    if compress(CD_PRODUIT,,'kad')='L004IV';
57    put CD_PRODUIT $hex32.;
58   run;

4C30303449562020



59   data x2;
60    set DONNEEMAQUETTE3_q;
61    if compress(CD_PRODUIT,,'kad')='L004IV';
62     put CD_PRODUIT $hex32.;
63   run;

4C30303449560D0A

Therefore, you need to use function COMPRESS to get rid of these characters:

proc sql;
	create table ACCORD13_1 as select  a.*, b.* 
	from ACCORD2_q as a 
	    left join DONNEEMAQUETTE3_q as b on compress(a.CD_PRODUIT,,'kad')=compress(b.CD_PRODUIT,,'kad')
	;
quit;
SASdevAnneMarie
Barite | Level 11
Thank you, Ksharp!
What means the 2d and 3d option (,,'kad') in compress(b.CD_PRODUIT,,'kad')?
SASdevAnneMarie
Barite | Level 11
Thank you, I did. I didn’t find « kad » modifier.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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