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

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;

 


 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 419 views
  • 2 likes
  • 4 in conversation