BookmarkSubscribeRSS Feed
agambaccini
Calcite | Level 5

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;

1 REPLY 1
art297
Opal | Level 21

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 968 views
  • 0 likes
  • 2 in conversation