BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sastuck
Pyrite | Level 9

Hello,

 

New SAS user here. I have many missing observations in my dataset (states which didn't report arrest data according to the codebook), and I am trying to delete these missing observations so that they don't influence my data. The variable of interest is called GRNDTOT. I would like to do something along the lines of:

 

data paper.arrests_schools_popc;
if GRNDTOT=0 then delete;
run;

 

But this isn't working. The key concern here is to make sure the rows and columns remain aligned (rather than truncating the GRNDTOT column (therefore confusing the whole dataset) but to drop the FIPS codes without GRNDTOT data. Any help is appreciated!

 

Thanks, 

 

SAstuck

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Since you posted a word file with a graph in it instead of showing any data it is hard to know exactly what you want.  If you want to post a photo of your data using the Photos icon on the menu bar.  If you want to post examples of your data use the Insert Code icon instead and post a data step to create a sample of your data.

 

Your code should DELETE observations where the GRAND_TOTAL variable is zero, but you did not apply it to any data.  So you just created an empty dataset with only one variable. (NOTE that DROP is used to eliminate variables, not observations.)

 

But are you sure you don't want to just code the values to missing instead?  That way the observation is not deleted just because one variable has a missing value.  So if for some reason your original data was coded with zero to represent misisng data then you could use a step like this to convert the zeros to missing values instead.  Most SAS procedures will handle missing values. 

 

data want ;
  set have ;
  if grand_total=0 then grand_total=.;
run;

 

 

View solution in original post

4 REPLIES 4
Reeza
Super User

Your code will probably work if you add a SET statement so it has input data to work. Otherwise the concept/approach appears correct.

 


@sastuck wrote:

Hello,

 

New SAS user here. I have many missing observations in my dataset (states which didn't report arrest data according to the codebook), and I am trying to delete these missing observations so that they don't influence my data. The variable of interest is called GRNDTOT. I would like to do something along the lines of:

 

data paper.arrests_schools_popc;
if GRNDTOT=0 then delete;
run;

 

But this isn't working. The key concern here is to make sure the rows and columns remain aligned (rather than truncating the GRNDTOT column (therefore confusing the whole dataset) but to drop the FIPS codes without GRNDTOT data. Any help is appreciated!

 

Thanks, 

 

SAstuck


 

PeterClemmensen
Tourmaline | Level 20

And by missing, you mean 0 values I take it? 🙂

 

Your code seems legit. You could also do something like this

 

data paper.arrests_schools_popc;
	set YourData(where=(GRNDTOT ne 0));
run;

 

 

Tom
Super User Tom
Super User

Since you posted a word file with a graph in it instead of showing any data it is hard to know exactly what you want.  If you want to post a photo of your data using the Photos icon on the menu bar.  If you want to post examples of your data use the Insert Code icon instead and post a data step to create a sample of your data.

 

Your code should DELETE observations where the GRAND_TOTAL variable is zero, but you did not apply it to any data.  So you just created an empty dataset with only one variable. (NOTE that DROP is used to eliminate variables, not observations.)

 

But are you sure you don't want to just code the values to missing instead?  That way the observation is not deleted just because one variable has a missing value.  So if for some reason your original data was coded with zero to represent misisng data then you could use a step like this to convert the zeros to missing values instead.  Most SAS procedures will handle missing values. 

 

data want ;
  set have ;
  if grand_total=0 then grand_total=.;
run;

 

 

Kurt_Bremser
Super User

Do not use MS Office files for posting code, log or data.

Convert SAS datasets to a data step by using the macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat..., and post the resulting code according to the advice in https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce. Use the same method for posting any code, and use the {i} icon for logs or other text that needs a fixed-width font and the formatting (indentation) preserved.

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
  • 4 replies
  • 5868 views
  • 4 likes
  • 5 in conversation