Generating date format column out of three separate columns in Stata - stata

I have three columns for each observation indicating day, month, and year. I would like to combine these three columns into one to indicate the date. I will later need to add a day or subtracts a day for observations, so I need to make sure the format of the new date column is date.
I'm programming this in Stata. I used mdy function, but it generates empty observations. And I'm not sure why it is not working.
Here is a snapshot of the data:

Stata function mdy should work if the format of "year" is changed from yy to yyyy. Thus we can add 1900 to the "year" variable and then use the function mdy to generate a new variable that indicates the date using three variables "day", "month", and "year". The following code shows how to use mdy function:
replace year = 1900+year
gen date = mdy(month,day,year)

Related

DAX Doesnt Support Comparison

I have a table of Employees. The Table has both current and budgeted employees. Each employees has a salary, start date and if applicable a term date associated with them.
I am attempting for project the salary spend for each month for the next year based on the following rules:
Hire_Date <= Projected_Month
Term_Date > Projected_Month
Measure:
CALCULATE(sum(INPUT_TECH[Salary_USD]),filter(INPUT_TECH,INPUT_TECH[Hire_Date]<=MEASURE_SWB_SPEND[MEASURE_SWB_SPEND_DATE]&&INPUT_TECH[Term_Date]>MEASURE_SWB_SPEND[MEASURE_SWB_SPEND_DATE]))
The issue here is that if ther is no Term_date, the cell will be blank, and if the cell is blank, the term_date will always be less than the current projected month and therefore the answer will always be 0.
I attempted to fix this by changing the format of the termdate to always either have the number 100,000 formated to read "ACTIVE" or the date they were actually terminated
Format: [=100000]"ACTIVE";[<>100000]mm/dd/yyyy
However, I still receive the following error:
DAX comparison operations do not support comparing values of type Text with values of type Date. Consider using the VALUE or FORMAT function to convert one of the values.
I would suggest not formatting the term date to the string "Active" and then using it for comparisons in your measures. This is why you may be getting that specific error. DAX is finding a data-type issue when making the comparisons. If you want the term to show up as "Active" where term dates are blank for visualization reasons, that may be okay. But I would establish a new calculated column that determines if a term date is blank and replaces it with a date type far into the future so that dates can be compared among dates—something like:
Term Date Blank Fix =
IF(
ISBLANK( INPUT_TECH[Term_Date] ),
// The third argument below could be replaced with a hard year, ex. 2099
DATE( 12, 31, YEAR( TODAY() ) + 10 ),
INPUT_TECH[Term_Date]
)
Then you could use the above column to make your comparisons in your measures. Keep in mind that if you are using a date table with AUTOCALENDAR() that making a date with the year 2099 may wildly expand your date table.

Dax TotalYTD and SamePeriodLastYear Not working

Hi I am trying to get total YTD amount for last year.
When I select October 2020 my current YTD amount works here is what it looks like :
YTDAmount = TOTALYTD(sum(PCSReport[PD]),PCSReport[PCSMonth])
However, I want to show YTD October 2019 and for some reason it is showing blank. This is the DAX I made :
LYTDAmount = TOTALYTD(sum(PCSReport[PD]), SAMEPERIODLASTYEAR(PCSReport[PCSMonth]) )
Since your calculations are correct, here is what might be the problem.
To work with time intelligence functions (TOTALYTD,DATESYTD ... ) you must have at leat one date table with an active relationship to your fact table.
A date table is a table that meets the following requirements:
It must have a column of data type date (or date/time)—known as the date column.
The date column must contain unique values.
The date column must not contain BLANKs.
The date column must not have any missing dates.
The date column must span full years. A year isn't necessarily a calendar year (January-December).
The date table must be marked as a date table.

How to extract Month and Year from column in PowerBI powerquery

I have a column (monthyear) in the image below. I want to extract the Month and year from the column to put it in the new column. Note: In my dataset this information goes for every day of the year
So the new column would look like:
01/2020
01/2020
01/2020
etc.
In Power Query, use some of the date functions.
To get the year it will be
Date.Year([monthyear])
For the month, it will depend on how you want to format it. Using the month of June as an example:
To get 'Jun'
Date.ToText([monthyear],"MMM")
To get the month number in the format 06
Number.ToText(Date.Month([monthyear]), "00")
Just to get the number 6 it will be:
Date.Month([monthyear])
In DAX use the date functions
For year the calculated column will be:
YEAR([monthyear])
For the month:
MONTH([monthyear])
I would always do a much data transformation in Power Query when you can before it gets to the data model.

How do I create a new variable with month and year instead of month, day and year

I imported my Excel sheet into Stata. I want to create a new variable, with month and year from an Excel column field called month_year.
So how do I create a date variable ith only month and year?
I have unsuccessfully tried to use the date() function which uses two arguments, one the variable name and the other the MDY like date(Date2,"MDY") like date(Date2,"MY") but it's not working. I am using Stata 13.
So I just found how this can be done. The code is as follows:
gen NEW_MONTH = monthly(MONTH, "M20Y")
format NEW_MONTH %tm
I then formatted into the required format from the new variable's properties to
Month Year

How to destring a date in Stata containing just the year?

I have a string variable in Stata called YEAR with format "aaaa" (e.g. 2011). I want to replace "aaaa" with "31decaaaa" and destring the obtained variable.
My feeling is that the best way to proceed could be firstly destringing the variable YEAR and then adding "31dec". To destring the variable YEAR I have tried the command date but it does not seem to work. Any suggestion?
It would be best to describe your eventual goal here, as use of destring just appears to be what you have in mind as the next step.
If your goal is, given a string variable year, to produce a daily date variable for 31 December in each year, then destring is not necessary. Here are three ways to do it:
gen date = daily("31 Dec" + year, "DMY")
gen date = date("31 Dec" + year, "DMY")
gen date = mdy(12, 31, real(year))
Incidentally, there is no likely gain for Stata use in daily dates 365 or 366 days apart, as they just create a time series that is mostly implicit gaps.
If your data are yearly, but just associated with the end of each calendar year, keep them as yearly and use a display format to show "31 Dec", or the equivalent, in output.
. di %ty!3!1_!D!e!c_CCYY 2015
31 Dec 2015
Detail. date() is a function, not a command, in Stata. We can't comment on "does not seem to work" as no details are given of what you tried or what happened. daily() is just a synonym for date().