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

Hello, 

Can you help me for the if statement of this program. my date format is yymmn6.

data data_IMF_PCSS;
set data_IMF_PCSS;

if sas_date  between '202001' and '202112'D then ID=1 else ID=0 ;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You cannot use use BETWEEN with an IF statement.  That will only work in a WHERE statement.

You need to end your IF statement with a semicolon.

 

If the variable has actual date values with the YYMMN6. format attached so that they display as values like 202203 then you need to use actual data values in your IF statement.

 

Not the string you used for the lower bound on the BETWEEN and not the invalid date literal you used as the upper bound.  The text used in a date literal has to be something the DATE informat will understand.

 

Try this:

data data_IMF_PCSS;
  set data_IMF_PCSS;
  if '01JAN2020'd <= sas_date  < '01JAN2022'd then ID=1;
  else ID=0 ;
run;

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

@sasphd wrote:
 

Hello, 

Can you help me for the if statement of this program. my date format is yymmn6.

data data_IMF_PCSS;
set data_IMF_PCSS;

if sas_date  between '202001' and '202112'D then ID=1 else ID=0 ;
run;

Date literals are ALWAYS this format 

'01JAN2022'd

You could use mixed or lower case, that will work. You could use two digit years (but don't). Other formatting of date literals will fail (as you have seen).

 

The format of the variable SAS_DATE is irrelevant. Just because SAS_DATE is yymmn6, the date literals should NOT and cannot be yymmn6., they must be in the format shown, 'DDMONYYYY'd

 

if sas_date  between '202001' and '202112'D then ID=1 else ID=0 ;

There are other SAS syntax errors in the above line of code as well, all of these syntax must be fixed, including the date literals.

--
Paige Miller
Tom
Super User Tom
Super User

You cannot use use BETWEEN with an IF statement.  That will only work in a WHERE statement.

You need to end your IF statement with a semicolon.

 

If the variable has actual date values with the YYMMN6. format attached so that they display as values like 202203 then you need to use actual data values in your IF statement.

 

Not the string you used for the lower bound on the BETWEEN and not the invalid date literal you used as the upper bound.  The text used in a date literal has to be something the DATE informat will understand.

 

Try this:

data data_IMF_PCSS;
  set data_IMF_PCSS;
  if '01JAN2020'd <= sas_date  < '01JAN2022'd then ID=1;
  else ID=0 ;
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!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 622 views
  • 2 likes
  • 3 in conversation