DATA Step, Macro, Functions and more

Replacing observation value with previous value based

Reply
Senior User
Posts: 1

Replacing observation value with previous value based

Hello all,

I have been using SAS 9.4 for ~9 months.

 

I am currently working with hospital discharge data and have run into a problem.

 

I have individuals who are distinct, but do not have the same unique ID i.e., the same individual has more than one "unique" id.


To correct this problem, I created a new unique id variable, and now want to assign that value to the all subsequent values (event =2 or 3) for the ID  for each time an indivudal appears in the data. 

 

I have been trying to use the retain statement in my program, but can not get that to work.

 

I want to end up with a dataset where I keep all rows, but where the newid variable is the same in rows where Last_name first_name

are the same and takes the value when event=1 .

 

The code I am using is below.

Any help would be much appreciated!!!!

 

proc sort data=dups1;
by last_name first_name birth_date  ADMIT_START_OF_CARE s;
run; 





DATA dups1;
SET dups1;
retain newid;

    BY last_name first_name birth_date ADMIT_START_OF_CARE s;
    if first.last_name then do;

      event=1;

            retain event;

      end;

      if first.last_name^=1 then event=event+1;

run;

DATA dups1;
SET dups1;
retain newid; 
     by last_name first_name birth_date event; 

 	if last.event  then do; 

	practiceid=newid; 

	end; 
run; 
Super User
Posts: 11,343

Re: Replacing observation value with previous value based

1. You really should provide some example data. It would give us something to test code an and head off questions that are answered with supplied data. This link https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... has instructions on how to create datastep text from your data set that you can post here to give us that data.

 

2. Where do you assign the value of newid? If it is in the Dups1 data set it will get reset from each record supplied by the SET statement negating the Retain set for the variable.

 

3. When developing a process it is seldom a good idea to use the Data Dups1; Set Dups1; type of code since the result destroys the incoming data. You may change such that you have to go way back in your process to get an "orginal" input to set.

 

4. Pay very close attention to which variable you are testing for First or Last. Using First.Last_name as you have below means that John, Jane and Fred would all be in the same "If First" block of code if their last name is Smith. To identify individuals you may need to be looking at Birth_date (assuming no one has entered an incorrect birth date for any individual).

 

Ask a Question
Discussion stats
  • 1 reply
  • 182 views
  • 0 likes
  • 2 in conversation