DATA Step, Macro, Functions and more

Select cases in grade 1 across school years

Reply
New Contributor
Posts: 3

Select cases in grade 1 across school years

I want to select only those cases that are in grade 1 in year 0405, and those that are in grade 1 in year 0506, and those that are in grade 1 in year 0607.
 
If I put a 'or' statement, it will give me out cases that are also in other grades, if I put an 'and' statement, no cases will be found.
What is the code to select cases across multiple years, but all should be in grade 1 at that particular year.
 

data total1; set total;

if (a1_grade_04050=1) or (a1_grade_05060=1) or (a1_grade_06070=1) then output ;

run;

 

data total1; set total;

if (a1_grade_04050=1) and (a1_grade_05060=1) and (a1_grade_06070=1) then output ;

run;

Trusted Advisor
Posts: 1,555

Re: Select cases in grade 1 across school years

Posted in reply to AnnThomas

Using OR should work.

If you get unwanted results please post example of your input and relevant output

to display why it is not wanted

New Contributor
Posts: 3

Re: Select cases in grade 1 across school years

2.jpg

New Contributor
Posts: 3

Re: Select cases in grade 1 across school years

Posted in reply to AnnThomas

Thank you shmuel and ballardw.

Data cannot be changed, neither can be variable names.

With the or statement I get the correct number of observations in the log file, however when I look at the tables, also other grades, such as 0 (which is kindergarten), grade 2, and grade 3 are included. And I want just grade 1.

Super User
Posts: 19,772

Re: Select cases in grade 1 across school years

Posted in reply to AnnThomas

Your criteria is that any of the values are 1. This means the other values can be values other than 1. 

 

Change your proc freq to be 

 

var1*var2*var3/list

 

Then you'll see that at least one is a 1 in all cases. 

Super User
Posts: 11,343

Re: Select cases in grade 1 across school years

Posted in reply to AnnThomas

You should describe, or better yet provide examples of, your data. The instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to create data step code that can be posted here to recreate your data set.

 

It will also help to provide an example of what the desired result for your example data would would be.

 

From the variables you show and your request "grade 1 in year 0405" part of the issue could well be the data structure. When variable names contain information you need to query or manipulate then that is a sign that the data should be restructured. In general a variable named Grade with values like 1 and another variable Year with values like '0405' or '0506' (if those indicate school year 2004 to 2005 or 2005 to 2006 that's another issue) then the desired result would be nearly trivial:

If grade=1 and year in ('0405' '0506' '0607');

Ask a Question
Discussion stats
  • 5 replies
  • 328 views
  • 0 likes
  • 4 in conversation