BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nishant77goel
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

You will get more responses if you provide a meaningful subject line which describes the actual problem. 

--
Paige Miller
mkeintz
PROC Star

@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.

--------------------------
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

--------------------------
Kurt_Bremser
Super User

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.

nishant77goel
Obsidian | Level 7
Added in the description section. Please check. Thanks for all your help!
Kurt_Bremser
Super User

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.

nishant77goel
Obsidian | Level 7

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 639 views
  • 2 likes
  • 4 in conversation