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
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();
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;
yes! that works. Sorry , i must have copied the old code. Thank you !!
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.
Look again. Both do while loops call the iterator next() method within the loop.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.