BookmarkSubscribeRSS Feed
sas-inquirer
Quartz | Level 8
@ballardw
In all cases where the key_var is new, it should be inserted in numeric order so 1-2-3 should come before 1-2-4. For existing key_var, they should be in the order specified by the sort_order variable. So the idea is to take the original report and manually insert the new points by examining the values and inserting them in numeric order. I have no control over the report design other than being the person who has to update it. Perhaps this is just something that has to be done on a case by case basis manually. Thanks for your assistance.
mkeintz
PROC Star

You don't need a sort order rule if the current master data set is (1) already in the desired order, and (2) all relevant values of the sort variable ("sortvar" below), are found at least once in the master data set.

 

You already stated that condition 1 is true.  So assuming (2) is also true, then:

 

data want;
  set master;
  by key_var notsorted;
  if _n_=1 then do;
    declare hash h (dataset:'newdata',multidata:'Y');
	  h.definekey('key_var');    /* Was "definesortvar"*/
	  h.definedata(all:'Y');
	  h.definedone();
  end;

  output;

  if last.key_var=1 and h.find()=0 then do until (h.find_next()^=0);
	output;
  end;
run;

Actually (2) doesn't have to be entirely true. What you really need is that the NEWDATA data set contains no key_var value not already in MASTER.

 

 

I've corrected some bad editing in my code above.  Also this program assumes that dataset NEWDATA has all the same variables as MASTER.  Otherwise extra variables from the last within-by-group observation in MASTER will be kept in the new observations added for the NEWDATA observations.

--------------------------
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-inquirer
Quartz | Level 8
@mkeintz This looks like an interesting approach and I am eager to try it, but I get an error (DATA STEP Component Object failure. Aborted during the COMPILATION phase.) From everything I read on google it would seem to point to a syntax error but unfortunately I have no experience with hash so am having trouble pin-pointing it. Any suggestions?
ballardw
Super User

@sas-inquirer wrote:
@mkeintz This looks like an interesting approach and I am eager to try it, but I get an error (DATA STEP Component Object failure. Aborted during the COMPILATION phase.) From everything I read on google it would seem to point to a syntax error but unfortunately I have no experience with hash so am having trouble pin-pointing it. Any suggestions?

ANY time you get an error you should copy the log with all the procedure text and all notes, warnings as well as the message, open a text box on the forum with the </> icon and paste the text. The text box is important because SAS often includes diagnostic information that depends text behavior and the message windows on this forum will reformat pasted text which makes those diagnostics less useful or easy to follow.

 

With that said, look in the log or your code for something that includes two . in the syntax like this.that.something.

The error you get most easily generated that way.

sas-inquirer
Quartz | Level 8
@mkeintz thanks for your code. I liked the approach. Unfortunatly I cannot make it work for my needs. I think this is a case where automation is not possible.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 19 replies
  • 3919 views
  • 7 likes
  • 5 in conversation