Starting with the SASHelp data set, Sales, create a temporary SAS data set (call it Sales) that contains a variable (call it Date) that is a SAS date by using the three variables Month, Day, and Year (remember the MDY function)

Starting with the SASHelp data set, Sales, create a temporary SAS data set (call it Sales) that contains a variable (call it Date) that is a SAS date by using the three variables Month, Day, and Year (remember the MDY function). Be aware that the original SASHelp data set also contains a variable called Date, but it is not a SAS date. You can drop the original Date variable with a DROP= data set option on the SET statement when you bring in data from the SASHelp data set. Next, generate a time series plot with Date on the x axis and Sales on the y axis. It should look like this:

The Correct Answer and Explanation is :

To create a temporary SAS data set named Sales that includes a SAS date variable derived from the existing Month, Day, and Year variables, and then generate a time series plot with Date on the x-axis and Sales on the y-axis, follow these steps:

  1. Create the Sales Data Set with a SAS Date Variable: Use the MDY function to combine the Month, Day, and Year variables into a new variable Date. The MDY function returns a SAS date value from the specified month, day, and year. citeturn0search2
   data Sales;
       set sashelp.sales(drop=Date);
       Date = mdy(Month, Day, Year);
       format Date date9.;
   run;

In this code:

  • set sashelp.sales(drop=Date); reads the sashelp.sales data set and drops the existing Date variable to avoid conflicts.
  • Date = mdy(Month, Day, Year); creates a new Date variable by combining Month, Day, and Year.
  • format Date date9.; formats the Date variable to display in the date9. format (e.g., 28FEB2025).
  1. Generate the Time Series Plot: Use the SGPLOT procedure to create a time series plot with Date on the x-axis and Sales on the y-axis. The series statement plots the data points connected by lines, which is suitable for time series data. citeturn0search1
   proc sgplot data=Sales;
       series x=Date y=Sales;
       xaxis label='Date';
       yaxis label='Sales';
   run;

In this code:

  • proc sgplot data=Sales; initiates the plotting procedure using the Sales data set.
  • series x=Date y=Sales; plots the Sales variable against Date, connecting the points with lines.
  • xaxis label='Date'; and yaxis label='Sales'; add labels to the x-axis and y-axis, respectively.

Explanation:

  • MDY Function: The MDY function in SAS is used to create a SAS date value from separate month, day, and year variables. It takes three arguments: month, day, and year, and returns a SAS date value representing that specific date. For example, mdy(2, 28, 2025) returns the SAS date value for February 28, 2025. citeturn0search2
  • SAS Date Values: SAS stores dates as the number of days since January 1, 1960. This internal representation allows for efficient date calculations and comparisons. When you create a date variable using the MDY function, SAS assigns it this numeric value, which can be formatted for display purposes.
  • Time Series Plot: The SGPLOT procedure is a versatile tool in SAS for creating various types of plots, including time series plots. The series statement is specifically designed for time series data, as it connects data points with lines, making it easier to visualize trends over time. The xaxis and yaxis statements allow you to customize the labels of the axes for better clarity. citeturn0search1

By following these steps, you can effectively create a new date variable from existing components and visualize the sales data over time using a time series plot in SAS.

Scroll to Top