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;
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;
					
				
			
			
				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;
					
				
			
			
				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
					
				
			
			
				
			
			
			
			
			
			
			
		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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.