A variable is to be created that contains unique values like 00001,00002 etc.
Now if all the unique values are assigned during execution, the value should not repeat when the code executes during next week execution. It should repeat only for cases where the key variable like member is same else it should assign different value from all previously assigned values. So how can we monitor all previously assigned values and if the member is same we assign same value else we assign different value for all future executions
data First_Execution;
input memberid 8.;
datalines;
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
;
data First_Execution_1;
set First_Execution;
FORMAT Source_ID $13.;
Source_ID= cats("777",put(_N_,z7.)) ;
run;
data Next_Execution;
input memberid 8.;
datalines;
1001
1002
1012
1004
1005
1011
1013
1015
1009
1015
;
data Next_Execution_1;
set Next_Execution;
FORMAT Source_ID $13.;
Source_ID= cats("777",put(_N_,z7.)) ;
run;
During next execution if the member is present in the previous execution source ID will be same, else it will be different. So how should we create a base of all previous executions and store this information.
See this:
data First_Execution;
input memberid 8.;
datalines;
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
;
data First_Execution_1;
set First_Execution;
FORMAT Source_ID $13.;
Source_ID= cats("777",put(_N_,z7.)) ;
run;
data Next_Execution;
input memberid 8.;
datalines;
1001
1002
1012
1004
1005
1011
1013
1015
1009
1015
;
data next_execution_1;
set next_execution end=done;
FORMAT Source_ID $13.;
if _n_ = 1
then do;
declare hash l (dataset:"first_execution_1");
l.definekey('memberid');
l.definedata('memberid','source_id');
l.definedone();
declare hash k (dataset:"first_execution_1");
k.definekey("source_id");
k.definedone();
end;
if l.find() ne 0
then do;
source_id = cats("777",put(_N_,z7.));
do until (k.check() ne 0);
_n_ = _n_ + 1;
source_id = cats("777",put(_N_,z7.));
end;
rc = l.add();
rc = k.add();
end;
if done
then do;
rc = l.output(dataset:"first_execution_2");
end;
run;
Once you are satisfied that the method works, you can change the dataset name in the output() method so that the lookup dataset is always overwritten and therefore current.
You will get more responses if you provide a meaningful subject line which describes the actual problem.
@PaigeMiller wrote:
You will get more responses if you provide a meaningful subject line which describes the actual problem.
I have read the problem description and still don't grasp the exact nature of the task. Sample data please.
So you already have a unique member id, but want to map that to a different key, probably for anonymisation purposes?
Please supply examples for your existing keys, in a data step with datalines, so we have something to build upon.
See this:
data First_Execution;
input memberid 8.;
datalines;
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
;
data First_Execution_1;
set First_Execution;
FORMAT Source_ID $13.;
Source_ID= cats("777",put(_N_,z7.)) ;
run;
data Next_Execution;
input memberid 8.;
datalines;
1001
1002
1012
1004
1005
1011
1013
1015
1009
1015
;
data next_execution_1;
set next_execution end=done;
FORMAT Source_ID $13.;
if _n_ = 1
then do;
declare hash l (dataset:"first_execution_1");
l.definekey('memberid');
l.definedata('memberid','source_id');
l.definedone();
declare hash k (dataset:"first_execution_1");
k.definekey("source_id");
k.definedone();
end;
if l.find() ne 0
then do;
source_id = cats("777",put(_N_,z7.));
do until (k.check() ne 0);
_n_ = _n_ + 1;
source_id = cats("777",put(_N_,z7.));
end;
rc = l.add();
rc = k.add();
end;
if done
then do;
rc = l.output(dataset:"first_execution_2");
end;
run;
Once you are satisfied that the method works, you can change the dataset name in the output() method so that the lookup dataset is always overwritten and therefore current.
Thanks for your help! I am new to Hash programming so will take time for me to understand this code, but it is perfectly doing what is needed
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.