BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aperansi
Quartz | Level 8

 

I have the variable "mid" that I want to set values to based on a specific date of the month.  The report runs for 12 months of data, so it will not just be one month in the report. In the data step before the one below I have the date coming in as yymmdd10. I want to set the mid variable to either read '1st through the 15th' if the date is between the 1st and the 15th of a given month, or the '16th through the 30/31st'.

 

I've tried using the below as a starting point with no luck.

 

data BankBoostDataSet;  /* dates and frequencies for every ID SCore product call since October 2017 */
  set BBID;
  length mid $30 product $30 vendor $30;
  vendor = 'MerchantBoost';
  if api_date month(date) < 16 then mid = '1st through the 15th';
  else mid = '16th through the 30/31st';
  product = 'BankBoost';
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Month() returns the month and 16 doesn't make sense. 

 

if api_date < month(16) then mid = '1st through the 15th';
  else if api_date >= month(16) then mid = '16th through the 30/31st';

 

 

It should be:

 

if day(api_date) < 16 then mid = '1st through the 15th';
  else if day(api_date) >= 16 then mid = '16th through the 30/31st';

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

We need to see test data in the form of a datastep, and what the output should look like.  Also explain what "did not work", were there errors, did it not work as expected etc.

aperansi
Quartz | Level 8

Here is the entire code for the program. I have also attached a picture of the output of what the final output is, but the mid column isn't populating correctly based on the dates. As it stands, there were no errors, and the program ran successfully. But my if statement to look at the date and determine if the date is less than the 16th of the month isnt working. Along with the else statement to assign the mid column with '16th through 30th' did not assign correctly.

 

 

 

Reeza
Super User

Month() returns the month and 16 doesn't make sense. 

 

if api_date < month(16) then mid = '1st through the 15th';
  else if api_date >= month(16) then mid = '16th through the 30/31st';

 

 

It should be:

 

if day(api_date) < 16 then mid = '1st through the 15th';
  else if day(api_date) >= 16 then mid = '16th through the 30/31st';
aperansi
Quartz | Level 8
Thank you for clearing that up for me Reeza. This worked!
ballardw
Super User

The value

month (16)

used in your code will always result in the value of 1. 16 corresponds to 16JAN1960 as far as the MONTH function is concerned.

So unless API_DATE is before 01JAN1960 (the value of such a date is numeric 1) then the comparison

api_date < month(16)

will never be true.

Similarly any API_DATE 01JAN1960 and greate will make this true:

api_date >= month(16)

 

You might want DAY(api_date) < 16 and DAY(api_date) ge 16

 

You may need to brush up on how SAS stores dates and what the functions actually return.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

ballardw
Super User

@aperansi wrote:

 

I have the variable "mid" that I want to set values to based on a specific date of the month.  The report runs for 12 months of data, so it will not just be one month in the report. In the data step before the one below I have the date coming in as yymmdd10. I want to set the mid variable to either read '1st through the 15th' if the date is between the 1st and the 15th of a given month, or the '16th through the 30/31st'.

 

I've tried using the below as a starting point with no luck.

 

data BankBoostDataSet;  /* dates and frequencies for every ID SCore product call since October 2017 */
  set BBID;
  length mid $30 product $30 vendor $30;
  vendor = 'MerchantBoost';
  if api_date month(date) < 16 then mid = '1st through the 15th';
  else mid = '16th through the 30/31st';
  product = 'BankBoost';
run;

 


See it this will get you started.

This will only work if your date variable is an actual SAS date value and not character value or a numeric that appears as 20180401 unless you have applied a SAS date format to create that appearance.

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