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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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