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;
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';
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.
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.
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';
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.
@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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.