BookmarkSubscribeRSS Feed
AnnThomas
Calcite | Level 5
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;

5 REPLIES 5
Shmuel
Garnet | Level 18

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

AnnThomas
Calcite | Level 5

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.

Reeza
Super User

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. 

ballardw
Super User

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');

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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