1

In a questionnaire I asked people for how long they ususally sleep a night. Now replys were supposed to be in a h.mm format but were accidently set to h only. This is why some participants gave their sleep duration in minutes or simply wrote "830" for 8 hours 30 minutes. Now I wanted to correct for both variations and tried this first:


RECODE Sleep (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10)(11=11)(12=12)(13=13)(14=14)(801 thru 899 = 8.5) (701 thru 799 = 7.5)(200 thru 600 = (Sleep/60))
 INTO Sleep_rek.

RECODE Sleep_rek(LOWEST thru 4.5 = 0) (5, 6= 1) (6.5 thru 7 = 2) (7.5 thru HIGHEST=3)
  INTO PSQI_K3_SleepDuration.


Execute. 

Since it didn't work and I thought maybe Recode can't do that, I tried this instead:


RECODE Sleep (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10)(11=11)(12=12)(13=13)(14=14)(801 thru 899 = 8.5) (701 thru 799 = 7.5)
 INTO Sleep_rek.

IF (Sleep = (300 thru 600) & Sleep_rek = sysmis) Sleep_rek = (Sleep/60).


RECODE Sleep_rek(LOWEST thru 4.5 = 0) (5, 6= 1) (6.5 thru 7 = 2) (7.5 thru HIGHEST=3)
  INTO PSQI_K3_SleepDuration.


Execute. 

However this threw an error as well:

The code is incomplete, check for missing operants, invalid operants, non-matching paranthesis or too long strings.

The PSQI_K3_SleepDuration variable in the end was computed but anyone with a 300 to 600 value is still a missing value.

Can anyone tell me how to put it so it will work?

eli-k
  • 10,898
  • 11
  • 40
  • 44
Ninke
  • 247
  • 1
  • 12

1 Answers1

1

I can see a few errors in your syntax - see if it works once you've corrected them.

  1. Indeed, recode .... (200 thru 600 = (Sleep/60)) can not work, you can't use a calculation as the target value.

  2. recode ... (5, 6= 1) should be (5 6= 1) instead (no comma needed).

  3. IF (Sleep = (300 thru 600) & Sleep_rek = sysmis) Sleep_rek = (Sleep/60) has a couple of errors: thru ia a subcommand for recode, can't be used like this, also can't use "sysmis" this way. Corrected version:

    IF (Sleep>= 300 and Sleep<=600) and missing(Sleep_rek) Sleep_rek = (Sleep/60).

  4. Your last recode is fine syntax-wise, but the ranges you are using seem to be wrong - since you'll be dividing numbers from 300 to 600 in 60, you will only get fractions in values 5 to 10. Yet the recode covers fractions in numbers under 4.5 and doesn't cover them in values between 5 to 7.5 (except specific values: 5, 6, 6.5, 7).

eli-k
  • 10,898
  • 11
  • 40
  • 44
  • Thank you for your very quick and helpful reply. Point three actually works and solves the problem. As for four yes, you're right of course, I wrote it when I didn't consider converting yet and overlooked it...Thanks for pointing that out! – Ninke Aug 03 '21 at 11:58