Using WHERE in PROC SQL Error - sas

Currently I'm trying to filter the output I have to display only products which have discounts greater than or equal to 60%. The variable discount lists all their values in the format such as 50%, 60% etc.
PROC SQL;
SELECT discount.Product_ID,Product_Name,Start_Date,End_Date,Discount
FROM Final.discount AS d, Final.product_dim AS p
WHERE d.Product_ID=p.Product_ID
AND Discount >= 60%;
QUIT;
I dont know why this isn't working, but the error from the log tells me that the percent sign isnt recognized? How would I fix this to get the output I desire?

Assuming the variable is numeric with the PERCENTw.d format applied (so likely PERCENT5. given your display), you would write it like so:
Discount >= 0.6;
The PERCENTw.d format displays numbers between 0 and 1 as 0% - 100%.
If your variable is character, then you would probably need to convert it to a number first (as '100%' > '60%' is false).
input(Discount, PERCENT5.) >= 0.6
This is well covered in Rick Wicklin's blog post on the Do Loop.

Related

Power BI: Format integer to string (conditional formatting)

Power Bi is getting data from MySQL from the table Vehicle Supports. In the website there is an enum of 1 = Yes and 0 = No.
I have a Donut chart which is displaying the integers since the data being saved in DB are integers.
I can't use conditional formatting as some tutorials mention since this is not a table. I also tried FORMAT() and didn't work.
How can I format the integers? If the number is 1 then change it to "Yes" and if the number is 0 then change it to No.
https://learn.microsoft.com/en-us/dax/if-function-dax
String = IF(number=1,"Yes","No")

In PowerBI what options are there to hide/redact/mask values in visuals to anonymise data

What options are there in PowerBI to suppress, redact or hide values to anonymise values in reports and visuals without loosing detail and have that restriction apply to multiple pages in a report?
Cat
Count
%
Category 1
23
10
Category 2
2
0.9%
Category 3
4
1.7%
So that its possible to keep the rows but end up with a placeholder where count is <4 and % is greater than 1% but less than 2%
Cat
Count
%
Category 1
23
10
Category 2
*
0.9%
Category 3
4
*
So far my experience has been
a measure with a filter applied will hide rows but you can't apply a measure filter to an entire page or all report pages.
Ive seen mention of conditional formatting to hide the value by having the font and background the same colour but that seems open to error and labour intensive.
I also want to be clear when a value has been suppressed or masked
I suspect there is a more better way but I haven't been able to figure out where to even start.
OK, I have something working but you will need Tabular Editor to create a calculation group. Here are the steps.
I'm using the following table (named "Table") as the source data.
Add two measures (calculation groups only work on measures) as follows.
% Measure = SUM('Table'[%])
Count Measure = SUM('Table'[Count ])
Open tabular editor and create a new calculation group named "CG" with a calculation item named "Mask". Paste the following code into the calculation item.
if (
(selectedmeasurename() = "% Measure" && selectedmeasure() >1 && selectedmeasure() <2)
||
(selectedmeasurename() = "Count Measure" && selectedmeasure() <4)
,"*",selectedmeasure()
)
4. Save the calculation group and in Power BI drag the name column onto the filter for all pages as follows, ensuring it is selected:
The masking will now happen across all reports automatically. Below you can see the same table on two different reports which have now been successfully masked.
It depends on your data connection type as to whether this is available, but a calculated column (instead of a measure) can be used as a filter at the "this page" or "all pages" level.
If this option is available, then you can find it right next to the "New Measure" field.
Using this and your sample data above, I created a couple of calculated columns and show the resulting table. You can then display these columns and use them as filters throughout the report. Your DAX may be slightly different depending on how the actual data is formatted and such.
Count Calculated Column
Masked Count =
IF(
'Table'[Count] < 4,
"*",
CONVERT('Table'[Count], STRING)
)
% Calculated Column
Masked % =
IF(
'Table'[%] > .01 && 'Table'[%] < .02,
"*",
CONVERT('Table'[%] * 100, STRING) & "%"
)
Resulting Table
Example of how the filter can be used
The values of these columns will update as your data source is refreshed in Power BI. However, calculated columns aren't available for Live Connection, in which case you would have to do this kind of logic at a lower level (in Analysis Services for example).
Additionally, you could potentially use Power Query Editor to accomplish this kind of thing.

Moving variable to next column in SAS

I'm having the following table in SAS
SAS Table: Price
ID Description Price Discount
20 Hot blue warm 12.0
21 Durable A 15.0 0
22 Flexible 13.5 0
23 Bendable and A 12.3
I'm planning to move 'warm' and 'and A' from Price column to Description column while '12.0' and '12.3' to Price, what should I do?
You cannot change the type of an existing variable, but you can change the name.
Use the INPUT() function to convert strings to numbers. You can use the ?? modifier to suppress errors generated by strings that do not represent numbers.
data want;
set price(rename=(price=char_price));
price = input(char_price,??32.)
if missing(price) then description=catx(' ',description,char_price);
run;

xline option when date is formatted %th?

I'm doing a connected twoway plot with x-axis as dates formatted as %th with values 2011h1 to 2017h2. I want to put a vertical line at 2016h2 but nothing I've tried has worked.
xline(2016h2)
xline("2016h2")
xline(date==2016h2)
xline(date=="2016h2")
I'm thinking it might be because I formatted dates with
gen date = yh(year, half)
format date %th
I think this is a MWE:
age1820 date
10.42 2011h1
10.33 2011h2
11.66 2012h1
11.01 2012h2
14.29 2013h1
10.95 2013h2
12.42 2014h1
7.04 2014h2
7.07 2015h1
6.95 2015h2
4 2016h1
8.07 2016h2
5.98 2017h1
3.19 2017h2
graph twoway connected age1820 date, xline(2016h2)
Your example will not really work as written without some additional work. I think in future posts you may want to shoot for a fully working example to maximize the chance that you get a good answer quickly. This is why I made up some fake data below.
Try something like this:
clear
set obs 20
gen date = _n + 100
format date %th
gen age = _n*2
display %th 116
display %th 117
tw connected age date, xline(116 `=th(2018h2)') tline(2019h1)
The crux of the matter is that Stata deals with dates as integers that have a special label attached to them by the format command (but not a value label). For example, 0 corresponds to 1960h1. In other words, you need to either:
tell xline() the number that corresponds to the date you want
use th() to figure out what that number is and force the evaluation inside xline().
use tline(), which is smart enough to understand dates.
I think the third is the best option.

Power BI chart to highlight and compare any three periods of data

Does Power BI have a chart that can highlight to compare three periods? In this case, they are current month, previous month, and the same period last year.
I have the three measures ready but couldn't find a chart type to highlight the periods I want to compare and grey out the rest.
I believe the key here is using a measure on the chart's color saturation field.
I was able to achieve the following where the bar colors are dependent on the date slicer:
I defined my measure like this:
Color =
VAR SelectedDate = SELECTEDVALUE(Cal[Month])
RETURN 0.5*(EOMONTH(SelectedDate, -12) IN VALUES(MTS[Date])) +
0.8*(EOMONTH(SelectedDate, -3 ) IN VALUES(MTS[Date])) +
1*(EOMONTH(SelectedDate, 0 ) IN VALUES(MTS[Date]))
where MTS is a monthly time series table and Cal is an unrelated calendar table that only has month ends. Only one of the conditions holds at a time and the constants in front just define where on the color range the result will land when the condition returns TRUE() (which gets coerced to be 1 when multiplying).
Once you've put a measure on the color saturation field, you can access the data color settings under the Format section:
If you are just looking to highlight three measures manually to distinguish from the others you can go into the Format tab, and under "Data colors" you can toggle on the "Show all" option and change them there.
You could also use a slicer.