Help using Base SAS procedures

Hash tables

Reply
Occasional Contributor
Posts: 8

Hash tables

Hello

 

I have a large dataset and I want to use hash tables to loop thru all my clients and their decision at certain time point. Once the client has made a decision, I want to keep it for the current time period until a new decision is made. See attachment for current data and desired data.

 

Thanks!

Super User
Posts: 19,768

Re: Hash tables

Your question is coming across a bit as more of a 'code my work for me' than a request for help. Can you post what you've tried and where you're having any issues?
Occasional Contributor
Posts: 8

Re: Hash tables

Here is the code I have so far. The questions I have is:

why isn't h.find_next() giving me the next value?

how can I replace the current decision by the decision from the previous line?

 

data test;

length client $1. time decision $9.;

input client time decision;

datalines;

A 1 Blank

A 2 DecisionX

A 3 Blank

B 1 DecisionY

B 2 Blank

B 3 DecisionZ

B 4 Blank

;

options mlogic ;

data hello;

dcl hash h(dataset:'test', multidata: 'y');

h.definekey('client');

h.definedata('client', 'time', 'decision');

h.definedone();

 

do while (not done) ;

set dup end =done;

rc = h.find();

if (rc = 0) then do;

put client= time= decision=;

rc = h.find_next();

do while(rc = 0);

put 'test' client= decision;

rc = h.find_next();

 

end;

end;

end;

run;

 

 

Super Contributor
Posts: 298

Re: Hash tables

What is your DUP data set? It is needed to understand your problem.

Occasional Contributor
Posts: 8

Re: Hash tables

no dup only test data as provided
Super User
Posts: 11,343

Re: Hash tables

Since you have

do while (not done) ; 
set dup end =done;
rc = h.find();

in your code that isn't working the contents of the data set DUP mentioned here could well be the issue.

 

Occasional Contributor
Posts: 8

Re: Hash tables

Unfortunately, the error persists even with this new code:
data test;
length client $1. time decision $9.;
input client time decision;
datalines;
A 1 Blank
A 2 DecisionX
A 3 Blank
B 1 DecisionY
B 2 Blank
B 3 DecisionZ
B 4 Blank

;

data hello;
dcl hash h(dataset:'test', multidata: 'y');
h.definekey('client');
h.definedata('client', 'time', 'decision');
h.definedone();

do while (not done) ;
set test end =done;
rc = h.find();
if (rc = 0) then do;
put client= time= decision=;
rc = h.find_next();
do while(rc = 0);
put 'test' client= decision;
rc = h.find_next();

end;
end;
end;
run;
Super Contributor
Posts: 298

Re: Hash tables

Is this you want? If not, let us know your issue.



267  data hello;
268     dcl hash h(dataset:'test', multidata: 'y');
269     h.definekey('client');
270     h.definedata('client', 'time', 'decision');
271     h.definedone();
272
273     do while (not done) ;
274        set test end =done;
275        rc = h.find();
276        if (rc = 0) then do;
277           put client= time= decision=;
278           rc = h.find_next();
279           do while(rc = 0);
280              put 'test' client= decision;
281              rc = h.find_next();
282
283           end;
284        end;
285     end;
286  stop;
287  run;

NOTE: There were 7 observations read from the data set WORK.TEST.
client=A time=1 decision=Blank
testclient=A Blank
testclient=A DecisionX
client=A time=1 decision=Blank
testclient=A Blank
testclient=A DecisionX
client=A time=1 decision=Blank
testclient=A Blank
testclient=A DecisionX
client=B time=1 decision=DecisionY
testclient=B Blank
testclient=B DecisionZ
testclient=B Blank
client=B time=1 decision=DecisionY
testclient=B Blank
testclient=B DecisionZ
testclient=B Blank
client=B time=1 decision=DecisionY
testclient=B Blank
testclient=B DecisionZ
testclient=B Blank
client=B time=1 decision=DecisionY
testclient=B Blank
testclient=B DecisionZ
testclient=B Blank
NOTE: There were 7 observations read from the data set WORK.TEST.
Super Contributor
Posts: 298

Re: Hash tables

&colon appears when posted. It should stand for colon(Smiley Happy.

Occasional Contributor
Posts: 8

Re: Hash tables

Unfortunately, what I'm looking for would look like this:
client=A time=1 decision=Blank
testclient=A DecisionX
testclient=A DecisionX
client=B time=1 decision=DecisionY
testclient=B DecisionY
testclient=B DecisionZ
testclient=B DecisionZ
Ask a Question
Discussion stats
  • 9 replies
  • 663 views
  • 0 likes
  • 4 in conversation