Kavitha
Kavitha

Reputation: 371

Dummy variable in SAS

I want to assign dummy variable as OP by SN and Name. If SN and Name then OP=1, if SN and Name<> to previous name then OP=2, if it repeats then OP=2, same condition follows,Please see the example below

 SN     NAME    OP
109034 SPANISH  1
109036 FRANCE   1
109036 SECURITE 2
109036 SECURITE 2
109036 SECURITE 2
109037 AOMlk    1
109037 SECPPAA  2
109037 CIVILE   3
109037 CIVILE   3
109038 FRANCE   1
109038 SECURITE 2
109038 SECURITE 2
109038 SECURITE 2
109039 GOVERNME 1
109039 GOVERNME 1
109039 GOVERNME 1
109041 ITALIA   1
109041 SOREML   2
109041 SOREML   2
109041 SOREML   2

I tried with this code, I am not getting what i want

data new2( keep=SN Name OP);
set new1;
by   SN  Name ;
retain OP(0);
if first.SN  and first.Name then OP=1;
else OP=OP+1;

run;

Upvotes: 1

Views: 215

Answers (1)

Leo
Leo

Reputation: 2135

The reason your code isn't working is because it's increment OP when the name doesn't change and setting it back to 1 when it does change.

You need something like the following to do what you're trying to do:

data want;
  set have;
  by sn name notsorted;

  if first.sn then op = 1;
  else if first.name then op + 1;
run;

Upvotes: 1

Related Questions