Field value replacement

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Field value replacement

Hi,

 

I have a field called PID. This field can have duplicate values. What I would to achieve is for the 1st PID value to return the value of 1 and if the next value is a duplicate of the 1st value, 2, but if the 3rd value is a duplicate of ethe 2nd value, then this returns a 2 and so on.

 

Data set is called: Lodgings

 

Cheers

Haydn


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Super User
Posts: 6,318

Re: Field value replacement

Make sure data is sorted.  This data step will make a new dataset that adds the variable INSTANCE that counts from 1 within each value of PID.

 

data want;
   set LODGINGS;
   by pid;
   if first.pid then instance=0;
   instance+1;
run;

View solution in original post


All Replies
Solution
‎09-25-2015 06:23 AM
Super User
Super User
Posts: 6,318

Re: Field value replacement

Make sure data is sorted.  This data step will make a new dataset that adds the variable INSTANCE that counts from 1 within each value of PID.

 

data want;
   set LODGINGS;
   by pid;
   if first.pid then instance=0;
   instance+1;
run;
Contributor
Posts: 37

Re: Field value replacement

Thanks for your responses. Worked a treat :-)

 

Cheers

Haydn

Frequent Contributor
Posts: 144

Re: Field value replacement

First of all, you must sort data

 

proc sort data=Lodgings;

  by PID;

run;

 

Then, you could create a value such as.

 

Data want;

  set Lodgings;

  by PID;/*eneable you to use first.Pid*/

  if first.PID then Duplicated=1;

  else Duplicated=2;

run;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 239 views
  • 3 likes
  • 3 in conversation