BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
CathyVI
Lapis Lazuli | Level 10

Hello,

 

Am using a where statement in a data step. My variable runs from '2000' to '2019'. I don't want to write all the years and I believe there may be a way in sas to do it quickly and effectively. All of the values of interest are sequential.

 

This attempt gave me an error.

 

data want;

set have;

where year in ( '2000' --'2019');

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Normally, you would never use a character variable to represent calendar or clock quantities, in this case the year.

 

Assuming you have numeric year values, then this works:

 

data want;
set have;
where 2000<=year<=2019;
run;

 

 

This also works

 

data want;
    set have;
    where year between 2000 and 2019;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Normally, you would never use a character variable to represent calendar or clock quantities, in this case the year.

 

Assuming you have numeric year values, then this works:

 

data want;
set have;
where 2000<=year<=2019;
run;

 

 

This also works

 

data want;
    set have;
    where year between 2000 and 2019;
run;
--
Paige Miller
Tom
Super User Tom
Super User

If the values are integers you can use : to indicate a sequence.

1    data want;
2      set sashelp.class;
3      where age in (11:13) ;
4    run;

NOTE: There were 10 observations read from the data set SASHELP.CLASS.
      WHERE (age=INT(age)) and (age>=11 and age<=13);
NOTE: The data set WORK.WANT has 10 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.06 seconds
      cpu time            0.03 seconds
Reeza
Super User

Since you have quotes, I assume that's a character variable? Convert it to numeric if that's valid and then use BETWEEN.

 

where input(year, 8.) between 2000 and 2019;

@CathyVI wrote:

Hello,

 

Am using a where statement in a data step. My variable runs from '2000' to '2019'. I don't want to write all the years and I believe there may be a way in sas to do it quickly and effectively. All of the values of interest are sequential.

 

This attempt gave me an error.

 

data want;

set have;

where year in ( '2000' --'2019');

run;

 


 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 982 views
  • 4 likes
  • 4 in conversation