Help using Base SAS procedures

Hash Object Using Add Method - Need Code Check

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 90
Accepted Solution

Hash Object Using Add Method - Need Code Check

Hello Everyone,

So I have the following table that I need to create a hash object from:

Continent ID

Continent Name

Location

91

North America

North

93

Europe

North

94

Africa

South

95

Asia

South

96

Australia/Pacific

South

This is my code so far:

data _null_ (keep=Continent_ID Continent_Name Location);

  length Continent_ID 3.;

  length Continent_Name $25;

  length Location $5;

  if _N_=1 then do;

  declare hash L();

  L.definekey('Continent_ID');

  L.definedata('Continent_Name','Location');

  L.definedone();

  L.add(key:96,data:'Australia/Pacific',data:'South');

  L.add(key:95,data:'Asia',data:'South');

  L.add(key:94,data:'Africa',data:'South');

  L.add(key:93,data:'Europe',data:'North');

  L.add(key:91,data:'North America',data:'North');

  call missing(Continent_ID,Continent_Name,Location);

  end;

  L.output(dataset:'continents');

  run;

proc print data=continents;

run;

The problem I am having right now is that I am not getting my Continent_ID to output with the rest of the hash object. I am able to get the Continent_Name and Location to print out, but I am not able to get the key to print out.

Could someone take a look at my code and let me know where I am going wrong?

Thanks,

Alisa


Accepted Solutions
Solution
‎04-19-2012 07:36 PM
Respected Advisor
Posts: 3,156

Re: Hash Object Using Add Method - Need Code Check

Posted in reply to InfoAlisaA

Alisa,

You only output every variable that you have defined as 'data', 'key' plays as an index, will not be part of your output data set if you skip it.

BTW, when doing 'data _null_', none of the data set options will make a difference, as you are NOT outputting anything anyway.

data _null_ /*(keep=Continent_ID Continent_Name Location)*/;

  length Continent_ID 3.;

  length Continent_Name $25;

  length Location $5;

  if _N_=1 then do;

  declare hash L();

  L.definekey('Continent_ID');

  L.definedata('Continent_Id','Continent_Name','Location');

  L.definedone();

  L.add(key:96,data:96,data:'Australia/Pacific',data:'South');

  L.add(key:95,data:95,data:'Asia',data:'South');

  L.add(key:94,data:94,data:'Africa',data:'South');

  L.add(key:93,data:93,data:'Europe',data:'North');

  L.add(key:91,data:91,data:'North America',data:'North');

  call missing(Continent_ID,Continent_Name,Location);

  end;

  L.output(dataset:'continents');

  run;

proc print data=continents;

run;

Haikuo

View solution in original post


All Replies
Solution
‎04-19-2012 07:36 PM
Respected Advisor
Posts: 3,156

Re: Hash Object Using Add Method - Need Code Check

Posted in reply to InfoAlisaA

Alisa,

You only output every variable that you have defined as 'data', 'key' plays as an index, will not be part of your output data set if you skip it.

BTW, when doing 'data _null_', none of the data set options will make a difference, as you are NOT outputting anything anyway.

data _null_ /*(keep=Continent_ID Continent_Name Location)*/;

  length Continent_ID 3.;

  length Continent_Name $25;

  length Location $5;

  if _N_=1 then do;

  declare hash L();

  L.definekey('Continent_ID');

  L.definedata('Continent_Id','Continent_Name','Location');

  L.definedone();

  L.add(key:96,data:96,data:'Australia/Pacific',data:'South');

  L.add(key:95,data:95,data:'Asia',data:'South');

  L.add(key:94,data:94,data:'Africa',data:'South');

  L.add(key:93,data:93,data:'Europe',data:'North');

  L.add(key:91,data:91,data:'North America',data:'North');

  call missing(Continent_ID,Continent_Name,Location);

  end;

  L.output(dataset:'continents');

  run;

proc print data=continents;

run;

Haikuo

Frequent Contributor
Posts: 90

Re: Hash Object Using Add Method - Need Code Check

Thank you! This helped a lot!:smileylaugh:

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 179 views
  • 0 likes
  • 2 in conversation