<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Is any way to add key that not in dataset to hash? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676375#M203929</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class end=end_of_class;
  length key $200;
  length vname $32;
  if _n_=1 then do;
    dcl hash h (ordered:'A',multidata:'Y');
      h.definekey('name','key');

      do while (vname^='key');
        call vnext(vname);
            if vname ne 'end_of_class' then 
	    h.definedata(vname);
      end;
      h.definedone();
  end;
  *... other code ...;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I made a small change as shown above.&lt;/P&gt;</description>
    <pubDate>Thu, 13 Aug 2020 03:14:14 GMT</pubDate>
    <dc:creator>Lee_wan</dc:creator>
    <dc:date>2020-08-13T03:14:14Z</dc:date>
    <item>
      <title>Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/675403#M203500</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is any way to add key that not in dataset to hash? Just like:&lt;/P&gt;
&lt;PRE&gt;dcl hash h(dataset:"sashelp.class”);
h.definekey("key","sex");
h.definekey(all:"Y");
h.definedone();&lt;/PRE&gt;
&lt;P&gt;I try to use _N_ ,but failed.&lt;/P&gt;
&lt;PRE&gt;dcl hash h(dataset:"sashelp.class”);
h.definekey("_N_","sex");
h.definekey(all:"Y");
h.definedone();&lt;/PRE&gt;
&lt;P&gt;Or I try to use in option, failed as well.&lt;/P&gt;
&lt;PRE&gt;dcl hash h(dataset:"sashelp.class(in=key)”);
h.definekey("key","sex");
h.definekey(all:"Y");
h.definedone();&lt;/PRE&gt;
&lt;P&gt;So is there any possible to add a key that does not exist in the dataset when I use the dataset option in hash？&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 16:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/675403#M203500</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-08T16:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/675423#M203517</link>
      <description>&lt;P&gt;Once you issue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;dcl hash h(dataset:"sashelp.class”);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you can't specify any variables in the definekey or definedata method that are not in sashelp.class.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do what you apparently want to do, you can either:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vt /view=vt;
  set sashelp.class;
  _n=_n_;
run;

data _null_;
  if 0 then set vt;
  if _n_=1 then do;
    declare hash h (dataset:'vt');
      h.definekey('sex','_n');
	  h.definedata(all:'Y');
	  h.definedone();
  end;
  *....  other code ...;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class end=end_of_class;
  _n=_n_;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('sex','_n');
      h.definedata('name','sex','age','height','weight','_n');
      h.definedone();
  end;
  h.add();
  if end_of_class;
  *..... other code ...;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Aug 2020 02:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/675423#M203517</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-09T02:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/675433#M203522</link>
      <description>&lt;P&gt;In addition to the fact that the load from dataset method would have no way to find a variable that doesn't exist there is also the problem that the hash object does not like to store records with missing values for one of the key variables. Where would the value of this new KEY variable come from?&amp;nbsp; Either create it in advance (possible with a view) or just load the observations explicitly in your data step that is creating the hash object.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Aug 2020 19:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/675433#M203522</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-08T19:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676052#M203786</link>
      <description>&lt;P&gt;Thanks.&lt;BR /&gt;&lt;BR /&gt;Final, I use the code as below;&lt;/P&gt;
&lt;PRE&gt;data a;
  length key $200.;
  if _N_=1 then do;
    if 0 then set sashelp.class;
    array c _character_;
    array n _numeric_;
    dcl hash h(ordered:'A',multidata:'Y');
    h.definekey("sex","key");
    do over c;
        h.definedata (vname(c));
    end;
    do over n;
       h.definedata (vname(n));
    end;
    h.definedone();
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Aug 2020 01:59:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676052#M203786</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-13T01:59:53Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676060#M203790</link>
      <description>&lt;P&gt;&lt;EM&gt;&lt;U&gt;&amp;gt;&amp;nbsp;Final, I use the code as below;&lt;/U&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please do not post code as text. Use the appropriate icon.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 03:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676060#M203790</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-08-12T03:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676315#M203914</link>
      <description>&lt;P&gt;You're making a character array and numeric array as a device to get variable names.&amp;nbsp; You can avoid that with the "call vnext()" call routine:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class end=end_of_class;
  length key $200;
  length vname $32;
  if _n_=1 then do;
    dcl hash h (ordered:'A',multidata:'Y');
      h.definekey('name','key');

      do while (vname^='vname');
        call vnext(vname);
	    h.definedata(vname);
      end;
      h.definedone();
  end;
  *... other code ...;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure the first instance of KEY precedes the first reference to VNAME.&amp;nbsp; That will force the call vnext to encounter key before vname.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Aug 2020 20:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676315#M203914</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-08-12T20:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676363#M203922</link>
      <description>Sorry, edited!</description>
      <pubDate>Thu, 13 Aug 2020 02:01:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676363#M203922</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-13T02:01:00Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676364#M203923</link>
      <description>Oh!&lt;BR /&gt;Learned new knowledge! Thanks!</description>
      <pubDate>Thu, 13 Aug 2020 02:01:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676364#M203923</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-13T02:01:31Z</dc:date>
    </item>
    <item>
      <title>Re: Is any way to add key that not in dataset to hash?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676375#M203929</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set sashelp.class end=end_of_class;
  length key $200;
  length vname $32;
  if _n_=1 then do;
    dcl hash h (ordered:'A',multidata:'Y');
      h.definekey('name','key');

      do while (vname^='key');
        call vnext(vname);
            if vname ne 'end_of_class' then 
	    h.definedata(vname);
      end;
      h.definedone();
  end;
  *... other code ...;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I made a small change as shown above.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 03:14:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-any-way-to-add-key-that-not-in-dataset-to-hash/m-p/676375#M203929</guid>
      <dc:creator>Lee_wan</dc:creator>
      <dc:date>2020-08-13T03:14:14Z</dc:date>
    </item>
  </channel>
</rss>

