BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mkeintz
PROC Star

The NEXT method is not a hash object method, it is a hash iterator method.  So the following fails:

 

declare hash h;

   h.definekey('key');

   h.definedata('data');

  h.definedone();

 

rc=h.next();         ==> this will fila

 

You have to create an iterator for hash object H, i.e. you need this

 

dechare hiter hi  ('h');

 

Then use the next method via the statement

 

    rc=hi.next();

 

regards,

Mark

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

thanks Mark. it is  accessing a hash interator and still fails. I think I tried using next() months ago and gave up on it. 

 

   if 0 then _color=color;
      format _date date9.; call missing(_date);
      dcl hash h1_colors(multidata:'n', ordered:'y', hashexp:4);
      dcl hiter hh1_colors('h1_colors');
      h1_colors.defineKey('id','color');
      h1_colors.defineData('_color','_date');
      h1_colors.defineDone();

 

Patrick
Opal | Level 21

@CP2

Try the last code version I've posted. I believe that's what you're after.

The code works totally fine in my environment using the data you've posted.

 

Here all together:

data have ;
informat date mmddyy10.;
input id $ date color $ expectedCombo $;
format date mmddyy10.;
datalines;
1321 07/10/2015 blue blue
1321 07/11/2015 blue blue
1321 07/12/2015 blue blue
1321 07/13/2015 blue blue
1321 08/18/2015 red blue-red
1321 08/19/2015 blue blue-red
1321 08/20/2015 blue blue-red
1321 08/21/2015 blue blue-red
1321 08/22/2015 blue blue-red
1321 09/15/2015 red blue-red
1321 09/16/2015 blue blue-red
1321 09/17/2015 blue blue-red
1321 09/18/2015 blue blue-red
1321 01/12/2016 red blue-red
1321 01/12/2016 blue blue-red
1321 01/13/2016 blue blue-red
1321 01/14/2016 blue blue-red
1321 01/15/2016 blue blue-red
1321 01/16/2016 blue blue-red
1321 01/17/2016 blue blue-red
1321 01/18/2016 blue blue-red
1321 01/19/2016 green blue-red-green
1321 01/19/2016 blue blue-red-green
1321 01/20/2016 blue blue-red-green
1321 01/21/2016 blue blue-red-green
1321 01/22/2016 blue blue-red-green
1321 01/23/2016 blue blue-red-green
1321 01/24/2016 blue blue-red-green
1321 01/25/2016 blue blue-red-green
1325 01/16/2016 blue blue-red
1325 01/17/2016 blue blue-red
1325 01/18/2016 blue blue-red
1325 01/19/2016 green blue-red-green
1325 01/19/2016 blue blue-red-green
1325 01/20/2016 blue blue-red-green
1325 01/21/2016 blue blue-red-green
1325 01/22/2016 blue blue-red-green
1325 01/23/2016 blue blue-red-green
1325 01/24/2016 blue blue-red-green
1325 01/25/2016 blue blue-red-green
;
run;


proc format;
  invalue color_interval (just upcase)
    'RED' = 45
    other = 30
  ;
quit;

data want(drop=_:);
  set have;
  by id date;
  length Combo $ 400;

  if _n_=1 then
    do;

      if 0 then _color=color;
      format _date date9.; call missing(_date);
      dcl hash h1_colors(multidata:'n', ordered:'y', hashexp:4);
      dcl hiter hh1_colors('h1_colors');
      h1_colors.defineKey('id','color');
      h1_colors.defineData('_color','_date');
      h1_colors.defineDone();

      dcl hash h1_rowsSameDate(multidata:'y', dataset:'have(obs=0)', hashexp:2);
      dcl hiter hh1_rowsSameDate('h1_rowsSameDate');
      h1_rowsSameDate.defineKey('id','date');
      h1_rowsSameDate.defineData(all:'y');
      h1_rowsSameDate.defineDone();

    end;

  /* new id: clear hash h1_colors() */
  if first.id then
    h1_colors.clear();

  /* new date: clear hash h1_rowsSameDate() */
  if first.date then
    h1_rowsSameDate.clear();

  /* keep hashes up to date if required */
  h1_colors.replace(key:id, key:color, data:color, data:date);
  if not first.date or not last.date then h1_rowsSameDate.add();

  /* populate combo */
  if last.date then
    do;
      _rc = hh1_colors.first();
      do while (_rc = 0);
        if intck('day',_date,date)<=input(color,color_interval.) then combo=catx('-',combo,_color);
        else h1_colors.delete();
        _rc = hh1_colors.next();
      end;
    end;

  if first.date and last.date then
    /* only 1 color for the date */
    do;
      output;
    end;
  else if last.date then
    do;
      /* multiple colors for the date */
      _rc = hh1_rowsSameDate.first();
      do while (_rc = 0);
        output;
        _rc = hh1_rowsSameDate.next();
      end;
    end;

run;

Capture.PNG

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

yes! that works. Sorry , i must have copied the old code. Thank you !!

CP2
Pyrite | Level 9 CP2
Pyrite | Level 9

any reason why I would get a mismatch data type error for the _date field when converting code to my real drug dataset? My drug data is in date9. format (16NOV2016) for a field named administrationdate. I changed all the 'date' to 'administrationdate' but left the _date field the same. 

mkeintz
PROC Star
Very weird! After all what is the iterator object without a working next method?

Regards,
Markk
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Patrick
Opal | Level 21

@mkeintz

Look again. Both do while loops call the iterator next() method within the loop.

mkeintz
PROC Star
Paatrack:

I believe CP2 was saying saying that the next method wasn't working on his machine, My comment was meant to be a sympathetic response, not to suggest that sas actually published an iterator without a next method.

not that your program doesn't work.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 37 replies
  • 1797 views
  • 5 likes
  • 4 in conversation