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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 629 views
  • 0 likes
  • 2 in conversation