DATA Step, Macro, Functions and more

recode variable and missing

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

recode variable and missing

Hi all, I want to compute incarceration weight gain using only valid values.

Values of 999  and . are considered invalid and should not be considered .

see below data

ID

prejail_weight

postjail_weight

100

120

65

101

80

140

102

200

175

103

110

178

104

999

999

105

.

90

106

75

999

107

189

76

108

69

89

109

125

110

110

105

85

111

210

210

 

Some tranformations are required as well.

1-weight gain is to be coded 00 if weight gain  is less than -15 pounds.

2-weight gain is  to be coded 99 if  weight  gain is-15 pounds or more.

3- weight gain is to be coded 99 if weight gain is missing.

 

Any help would be appreciated.

Thanks,

 


Accepted Solutions
Solution
‎03-20-2016 05:25 PM
Super User
Posts: 19,869

Re: recode variable and missing

Posted in reply to archibald

If you're going to analyze this data, I don't recommend doing this. 

 

You'll end up having to filter out the 0 and 99 for all your analytical procs, but it's better to leave them as missing in my opinion.

SAS has several ways to identify missing values, so you may to consider using those instead.

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000992455.htm

 

 

@RW9 code seems correct, does it not do what you'd like?

 

Check for missing values, if there are any, don't calculate the weight_gain since you can't. 

Then code them to different missing values. 

I've used different variable names since it's easier to type out so make sure to replace the variables with yours as required.

 

if nmiss(post, pre) =0 then weight_gain = post - pre;

if 0 < weight_gain < 15 then weight_gain = .A;
else if weight_gain >  15 then weight_gain = .B;
run;

View solution in original post


All Replies
Super User
Posts: 19,869

Re: recode variable and missing

Posted in reply to archibald

Which part are you having trouble with? You say incarceration weight gain, will that be post-pre?

 

Can you post the code you have so far? You mention changing values to 99, would those then be considered invalid?

Contributor
Posts: 62

Re: recode variable and missing

I am having some issues when it comes to code the difference(post-pre weight). the value 99 of (post-pre weight) should be considered missing.
See specifications below:

1-weight gain is to be coded 00 if weight gain is less than -15 pounds.
2-weight gain is to be coded 99 if weight gain is-15 pounds or more.
3- weight gain is to be coded 99 if weight gain is missing.
4- else weight gain =weight gain
Solution
‎03-20-2016 05:25 PM
Super User
Posts: 19,869

Re: recode variable and missing

Posted in reply to archibald

If you're going to analyze this data, I don't recommend doing this. 

 

You'll end up having to filter out the 0 and 99 for all your analytical procs, but it's better to leave them as missing in my opinion.

SAS has several ways to identify missing values, so you may to consider using those instead.

http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000992455.htm

 

 

@RW9 code seems correct, does it not do what you'd like?

 

Check for missing values, if there are any, don't calculate the weight_gain since you can't. 

Then code them to different missing values. 

I've used different variable names since it's easier to type out so make sure to replace the variables with yours as required.

 

if nmiss(post, pre) =0 then weight_gain = post - pre;

if 0 < weight_gain < 15 then weight_gain = .A;
else if weight_gain >  15 then weight_gain = .B;
run;
Contributor
Posts: 62

Re: recode variable and missing

Thank you very much, Reeza
Super User
Super User
Posts: 7,994

Re: recode variable and missing

Posted in reply to archibald

Hi,

 

Not sure what the problem is?  Is seems to be a simple case of set the code column to 99, and then if weight gain is less than 15 set it to 00:

data have;
  input id prejail_weight postjail_weight;
datalines;
100 120 65
101 80 140
102 200 175
103 110 178
104 999 999
105 . 90
;
run;

data want;
  set have;
  code="99";
  if (prejail_weight not in (.,999) and postjail_weight not in (.,999)) then do;
    if postjail_weight-prejail_weight < 15 then code="00";
  end;
run;
Contributor
Posts: 62

Re: recode variable and missing

the code does not work. the difference post weight and pre weight returns 2 frequencies 00 and 99for all the cases.
I need to have an output similar to the one below where difference between post weight and pre weight is coded 00 and 99 as needed , else the values stay unchanged.

weight_gain
60
00
68
924
99
20
00
99

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 395 views
  • 1 like
  • 3 in conversation