BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dxiao2017
Lapis Lazuli | Level 10

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;

dxiao2017_2-1751652922575.png

dxiao2017_4-1751653012061.png

 

SAS help cars; we are cars; that is why my default image;
Tom
Super User Tom
Super User

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)))

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 6983 views
  • 10 likes
  • 6 in conversation