The last day of month with a given time flow in SAS - sas

I am bothered with a simple question.
I need to get the last business day of the month and the date in my dataset only includes the business day.
For example:
ID Date
1 20180301
1 20180302
1 20180305
...
1 20180329
1 20180330
1 20180402
...
2 20180301
2 20180302
2 20180305
And I need the output like this:
ID Date Enddate
1 20180301 20180330 (The last business of March)
1 20180302 20180330
1 20180305 20180330
...
1 20180329 20180330
1 20180330 20180330
1 20180402 20180430 (The last business of March)
...
2 20180301 20180330 (Same for other IDs)
2 20180302 20180330
2 20180305 20180330
I tried to use this command:
enddt=intnx('month',date,0,'E');
However, it will output 20180331 instead of 20180330.
So I was wondering if there is a method to extract directly the last day of given month instead of the calendar month.
Thank you very much for your kind help.

You can do this in a data step:
1) sort on date from latest to earliest (reverse sort)
2) create a variable based on the yearmonth = int(date/100)
3) do a datastep by yearmonth and retain enddate
4) if first.yearmonth then enddate = date;
5) drop vars you don't want
6) sort back to original order

Related

Power BI | group by in DAX

I have a "data" table and there is an column "period" -
period
1 year
3 year
1 year
2 year
3 year
3 year
I want my output to be -
year
count
1 year
2
2 year
1
3 year
3
please help me to get this output
Create a new table:
newTable = SUMMARIZE(
Tabelle, /// select table
Tabelle[period], ///
"Count", /// give name for value col
COUNT(Tabelle[period])) /// define function
Output:

Creating a calculated table by passing a measure or parameter

I have a requirement where I have a data like this,
Date Name Age
1-1-2018 A 1
2-2-2018 B 1
3-3-2018 B 1
6-6-2018 C 2
7-7-2018 B 6
I am trying to give a slicer to the user to select the required number of months from the last month.
So to do that, I am using a calculated column like this:
Month Year = DATEDIFF((Table1[Date]), TODAY(), MONTH) + 1
So that changes the data to something like this:
Date Name Age MonthYear
1-1-2018 A 1 7
2-2-2018 B 1 6
3-3-2018 B 1 5
6-6-2018 C 2 2
7-7-2018 B 6 1
The user selects the Month Year from the Slicer.
For example, when he selects 2, I want to display the last 2 months records in the table.
Expected Output:
Date Name Age
6-6-2018 C 2
7-7-2018 B 6
This works for me if I hardcode it like this:
Calculated Table = CALCULATETABLE(Table1,
FILTER(Table1, OR(Table1[MonthYear] > 2, Table1[MonthYear] = 2)))
But it fails when I try to pass the value in the place of 2 dynamically through a measure using SelectedValue function.
Calculated columns and calculated tables cannot reference a slicer value since they are only computed when you load your data.
If you want to apply this filtering to a visual, I'd suggest creating a separate table for your slicer. For example, you could use Months = GENERATESERIES(1,12) and then rename the column Months as well.
Use the Months[Months] column for your slicer and then create a measure which references it to filter your table/matrix visual.
Filter = IF(SELECTEDVALUE(Months[Months]) >= MAX(Table1[Month Year]), 1, 0)
Then use that measure in your Visual level filters box:

Calculate latest date for every index with DAX

I have a dataset in Power BI like this
ID DATE
1 06/24/2016
1 06/24/2017
1 06/24/2018
2 08/08/2017
2 08/08/2016
3 12/12/2015
I would like to create a calculated column with DAX, in which i have to calculate the latest date for every index, but I am only able to get the latest date of all the dataset or the same date for every row.
The output should be this:
ID DATE MAXDATE
1 06/24/2016 06/24/2018
1 06/24/2017 06/24/2018
1 06/24/2018 06/24/2018
2 08/08/2017 08/08/2017
2 08/08/2016 08/08/2017
3 12/12/2015 12/12/2015
MAXDATE = CALCULATE(MAX(Table1[Date]);FILTER(Table1;Table1[ID]=EARLIER(Table1[ID])))

How to identify if an observation is repeated every day in Stata

I have a database where I have a date variable, an id variable and a city variable. Sometimes the id variable is repeated in the same date and city.
Data looks something like this:
Date ID City
2/1/2015 1 1
2/1/2015 1 1
2/1/2015 1 2
2/2/2015 1 1
2/1/2015 2 1
2/2/2015 2 1
I would like to know how much days each ID is present, identify the id's that are present every day, and later on, those that are present every day in every city.
In the example above both ID 1&2 are present each day, but only ID 1 is present in each city each day.
Thanks!
I think I just did what i wanted to do.
All I had to do was:
by ID city date, sort: gen nvals = _n == 1
by ID city: replace nvals = sum(nvals)
by ID city : replace nvals = nvals[_N]

Run a regression of countries by quartiles for a specific year

I am exploring an effect that I think will vary by GDP levels, from a data set that has, vertically, country and year (1960 to 2015), so each country label is on 55 rows. I ran
sort year
by year: egen yrank = xtile(rgdp), nquantiles(4)
which tags every year row with what quartile of GDP they were in that year. I want to run this:
xtreg fiveyearg taxratio if yrank == 1 & year==1960
which would regress my variable (tax ratio) against some averaged gdp data from countries that were in the bottom quartile of GDPs in 1960 alone. So even if later on they grew enough to change ranks, the later data would still be in the regression pool. Sadly, I cannot get this code, or any variation, to run.
My current approach is to try to generate some new variable that would give every row with country label X a value of 1 if they were in the bottom quartile in 1960, but I can't get that to work either. i have run out of ideas, so I thought I would ask!
Based on your latest comment, which describes the (un)expected behavior:
clear
set more off
*----- example data -----
input ///
country year rank
1 1960 2
1 1961 1
1 1962 2
2 1960 1
2 1961 1
2 1962 1
3 1960 3
3 1961 3
3 1962 3
end
list, sepby(country)
*----- what you want -----
// tag countries whose first observation for -rank- is 1
// (I assume the first observation for -year- is always 1960)
bysort country : gen toreg = rank[1] == 1
list, sepby(country)
// run regression conditional on -toreg-
xtreg ... if toreg
Check help subscripting if in doubt.