Desktop productivity for business analysts and programmers

Multiple Data Steps...

Reply
N/A
Posts: 1

Multiple Data Steps...

How do I fix this code so that the data steps tell it to first select out and name the weapons and then out of those weapons that were named select out the specific reporting districts only and name 'YESWITHREP'.

Right now it is naming all reporting districts that are in the list and it is not looking at the weapons field first. 

DATA sped;

SET sped;

If incidenttype = '3700';

If weapontype = '3711' then weap = 'HANDGUNS    ';

Else if weapontype = '3712' then weap = 'RIFLESHOTGUN';

If weap = 'HANDGUNS    ' and REPDIST = 11 or REPDIST = 15

or REPDIST = 52 or REPDIST = 62 or REPDIST = 64 or REPDIST = 80

or REPDIST = 89 or REPDIST = 93 or REPDIST = 103 then gfsa = 'YESWITHREP';

If weap = 'RIFLESHOTGUN' and REPDIST = 11 or REPDIST = 15 or REPDIST = 52 or REPDIST = 62 or REPDIST = 64 or REPDIST = 80 or

REPDIST = 89 or REPDIST = 93 or REPDIST = 103 then gfsa = 'YESWITHREP';

ELSE gfsa = 'YESWOREP';

truants = -1;

Run;

Esteemed Advisor
Posts: 7,296

Multiple Data Steps...

I would think the same way that you have, i.e., by adding one more if statement.

However, I would create a new file rather than replacing your current file, so that you can test it.

Also, I personally, like to enter such statements in a way that makes it clearer what the various operators are supposed to do.  e.g.:

data sped;

  input (incidenttype weapontype) ($) repdist;

  cards;

3700 3711 15

3701 3711 15

3700 3711 15

3701 3711 15

3700 3712 15

3700 3712 80

3700 3712 93

3700 3712 105

;

DATA sped2;

  SET sped;

  If incidenttype = '3700';

  If weapontype = '3711' then weap = 'HANDGUNS    ';

   Else if weapontype = '3712' then weap = 'RIFLESHOTGUN';

  If weap = 'HANDGUNS    ' and REPDIST in (11,15,52,62,64,

        80,89,93,103) then gfsa = 'YESWITHREP';

  else if weap = 'RIFLESHOTGUN' and REPDIST in (11,15,52,62,64,

        80,89,93,103) then gfsa = 'YESWITHREP';

  ELSE gfsa = 'YESWOREP';

  truants = -1;

  if gfsa eq 'YESWITHREP';

Run;

Ask a Question
Discussion stats
  • 1 reply
  • 105 views
  • 0 likes
  • 2 in conversation