Help using Base SAS procedures

Modify a value in the data step with same variable name

Accepted Solution Solved
Reply
Contributor
Posts: 31
Accepted Solution

Modify a value in the data step with same variable name

Happy Holidays!

Could you please explain why the value TYPE after the modification couldn’t give correct answer?  The correct answer should be like this but the code didn't give this.  Why?  Thank you!

Type Frequency

.                  1

3                1

5                4

data lk;
   input id type;
datalines;
1 1
2 2
3 3
;
run;
data test;
input id;
datalines;
1
1
2
2
3
4
;
run;

data test1;
merge test(in=in1) lk;
by id;
if in1;

  if type in (1,2) then type=5;
  else if type ^= 3 then type=.;
run;
proc freq data=test1; tables  type/list missing; run;


Accepted Solutions
Solution
‎12-20-2013 01:19 PM
PROC Star
Posts: 7,468

Re: Modify a value in the data step with same variable name

You are retaining the modified value.  It will do as you expect if you use:

data test1 (drop=_type);

  merge test(in=in1) lk (rename=(type=_type));

  by id;

  if in1;

  if _type in (1,2) then type=5;

  else if _type ^= 3 then type=.;

  else type=3;

run;

View solution in original post


All Replies
Solution
‎12-20-2013 01:19 PM
PROC Star
Posts: 7,468

Re: Modify a value in the data step with same variable name

You are retaining the modified value.  It will do as you expect if you use:

data test1 (drop=_type);

  merge test(in=in1) lk (rename=(type=_type));

  by id;

  if in1;

  if _type in (1,2) then type=5;

  else if _type ^= 3 then type=.;

  else type=3;

run;

Contributor
Posts: 31

Re: Modify a value in the data step with same variable name

Thank you!  It should be the reason. 

Trusted Advisor
Posts: 1,137

Re: Modify a value in the data step with same variable name

You have used type instead of id in the if condition.

data test1;
merge test(in=in1) lk;
by id;
if in1;

  if id in (1,2) then type=5;

  else if id ^= 3 then type=.;

run;

proc freq data=test1; tables  type/list missing; run;

Please used the above code to get the desired result.

Thanks,

Jag

Thanks,
Jag
🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 196 views
  • 0 likes
  • 3 in conversation