Hi this is the answer base on others' reply and codes:
data class;
input Name$ Sex$ Age Height Weight;
datalines;
Alfred M 14 69.0 112.5
Alice F 13 56.5 84.0
Barbara F 13 65.3 98.0
Carol F 14 62.8 102.5
Henry M 14 63.5 102.5
;
run;
data cities;
input zipcode City :$30. Department :$30. Persona:$10.;
infile datalines dlm=',' dsd;
datalines;
75008, Paris 8è ,Paris,Alice
91940, Les Ulis, Essonne,Carol
92330,Sceaux, Hauts-de-Seine,Henry
93140, Bondy , Seine-Saint-Denis,Henry
94150, Rungis, Val-de-Marne,Alice
;
run;
proc print data=class;run;
proc print data=cities;run;
data set1;
if _n_=1 then do;
if 0 then set class(keep=name sex age);
declare hash match1(dataset:'class');
match1.definekey('name');
match1.definedata('sex','age');
match1.definedone();
end;
set cities(keep=persona department);
name=persona;
rc=match1.find(key:name);
if rc=0;
drop persona rc;
run;
proc print data=set1;run;
That seems to work.
The extra assignment statement
name=persona;
will prevent the issue mentioned above about values of PERSONA that are longer than the 8 bytes reserved for the NAME variable used as the key in the hash object. The assignment operation will truncate the value to fit into the NAME variable.
Note you could have done that in the .FIND() method call and avoided the need for the assignment statement. If you know the length needed you could put PUT function with $ format.
match1.find(key: put(persona,$8.) )
And if you don't then use VLENGTH() to find it and use the SUBSTRN() function to truncate.
match1.find(key:substrn(persona,1,vlength(name)))
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.
Ready to level-up your skills? Choose your own adventure.