sas: how to informat and format variable names as date - date

I got a data with some variable names are dates, but it cannot show date format when importing in sas. How can I format them as datetime when they are variable names?
The original Excel data like this:
acc_items
name
31/12/2017
31/12/2018
COMA - OPERATING PROFIT MARGIN
COMA
13344
37578
COMA - MARKET CAPITALIZATION
COMA
7522
9763
COMB - OPERATING PROFIT MARGIN
COMB
65456
76962
COMB - MARKET CAPITALIZATION
COMB
7665
9983
I write the code as this:
proc import datafile="C:\Users\sampledata"
dbms=xlsx replace
out=test;
run;
But when importing in sas, the data shows as following:
acc_items
name
_43100
_43465
COMA - OPERATING PROFIT MARGIN
COMA
13344
37578
COMA - MARKET CAPITALIZATION
COMA
7522
9763
COMB - OPERATING PROFIT MARGIN
COMB
65456
76962
COMB - MARKET CAPITALIZATION
COMB
7665
9983
Can anyone help? Thanks a lot!

When you store dates in a character field (variable names are character strings) in Excel then SAS converts them to digits that represents the internal number Excel uses to represent that date.
Since you should be storing data like dates in variables and not in metadata (variable names) just transpose the data to get the names into a variable. You can then convert the names back into dates.
proc transpose data=test out=tall;
by acc_items name notsorted;
var _: ;
run;
data want;
set tall;
date = input(compress(_name_,'_'),32.)+'30DEC1899'd ;
format date date9.;
rename col1 = amount;
drop _name_;
run;
Results
Obs acc_items name amount date
1 COMA - OPERATING PROFIT MARGIN COMA 13344 31DEC2017
2 COMA - OPERATING PROFIT MARGIN COMA 37578 31DEC2018
3 COMA - MARKET CAPITALIZATION COMA 7522 31DEC2017
4 COMA - MARKET CAPITALIZATION COMA 9763 31DEC2018
5 COMB - OPERATING PROFIT MARGIN COMB 65456 31DEC2017
6 COMB - OPERATING PROFIT MARGIN COMB 76962 31DEC2018
7 COMB - MARKET CAPITALIZATION COMB 7665 31DEC2017
8 COMB - MARKET CAPITALIZATION COMB 9983 31DEC2018
If you want to print your data in the same style as the original Excel report then use PROC REPORT and define DATE as an ACROSS variable.
proc report split='00'x data=want;
column acc_items name amount,date;
define acc_items/group;
define name/group;
define amount/sum ' ' format=comma10.;
define date/across ' ' format=ddmmyy10.;
run;
Results
acc_items name 31/12/2017 31/12/2018
COMA - MARKET CAPITALIZATION COMA 7,522 9,763
COMA - OPERATING PROFIT MARGIN COMA 13,344 37,578
COMB - MARKET CAPITALIZATION COMB 7,665 9,983
COMB - OPERATING PROFIT MARGIN COMB 65,456 76,962

Related

PostgreSQL rounding error | Sum of differences between values and difference between sum of values are not equal

I am trying to evaluate numbers upto 3 decimal precision. The formulas are fairly simple but sum of differences between values and difference between sum of values are not equal.
The dataset contains nearly 2050 entries.
result_column_1 = SUM(column1) - SUM(column2)
result_column_2 = SUM(column1-column2)
I am getting a huge dissimilarity between result_column_1 and result_column_2
column1 and column2 are already truncated (not rounded) to 3 decimal places.
1. ROUND() - didn't work
2. CAST TO Demical, Numeric - didn't work
3. TRUNC() - didn't work

Plot Two Regression Lines on Same Scatter Plot By Year: X-Axis Date MM/DD

I have a scatter plot of calls / time. My x variable is the date (Day/Month) and my Y variable is a number of calls on each date. I would like to plot two regression lines using PROC SGPLOT REG, one for 2019 and one for 2020. However, when I try to do this, all I get is a regular scatter plot with no regression lines. Here is my code:
proc sgplot data=intern.bothphase1;
reg x=date y=count / group=Year;
label count="Calls Per Day" year="Year";
Title "Comparison of EMS Calls per Day 1/1 - 3/31 in 2019 vs.
2020";
run;
The scatter plot comes up without issue (2019 and 2020 values in different colors) but I want to see how the trends differed between the two time periods, so I really want to get the regression lines on there. Can anyone help?
I imagine this has to do with the fact that I concatenated my day and month with a / so it is a character variable and so SAS cannot calculate the regression. I did this so I could use year as a class variable. I still have the original date variable in my table, is there a way I could get SAS to give me the month/day from that as a numeric variable?
Thanks!
EDIT: I used a date value in SAS and changed the format to mm/dd, but this doesn't help because the regression lines are just on either end of the graph rather than overlapping (picture attached). what I want is to have the regression lines overlap for the same time period 2019 vs. 2020 This is because SAS dates correspond to numbers from 1/1/1960. What I want is the mm/dd to correspond to numbers 1-365 so I get two overlapping regression lines to show how the trends changed from one year to the next. Anyone know how I can do this?
So two steps here: first, you need to generate a "day" value that's 1-365... so let's just subtract out 01JAN from the day value.
data have;
do date = '01JAN2019'd to '31DEC2020'd;
count = 25+2*rand('uniform');
year = year(date);
if month(date) le 3 then output;
end;
format date date9.;
run;
data adjusted;
set have;
date_fixed = date - intnx('year',date,0,'b') + 1; *current date minus jan 1 plus 1 (otherwise off by 1);
format date_fixed date5.; *this does not actually affect the graph axis, oddly;
run;
proc sgplot data=adjusted;
reg x=date_fixed y=count / group=Year;
xaxis valuesformat=date5.; *this seems to be needed for some reason;
label count="Calls Per Day" year="Year";
Title "Comparison of EMS Calls per Day 1/1 - 3/31 in 2019 vs.
2020";
run;
Then we add the xaxis line because for some reason it won't obey the DATE5. format (could also use MMDDYY5. as Reeza noted in comments, but we can force it to here.
Here is what I get. You can use other axis options to further limit things, so for example 01APR doesn't show up.
)

Crystal Reports 2013: Adding Leading Zeros to Sum in Group Footer

I have a report in Crystal Reports 2013 that is grouping multiple transactions into an aggregate transaction by account number.
The value is Amount, so in the Group Footer is it listed as SumofAmount.
The field must be 17 characters long, so any number must be padded with leading zeros.
The values are assumed to be decimal, so they are all integers.
For example, the following transformations would occur:
3123 needs to be: 00000000000003123
23283792387 needs to be: 00000023283792387
If I right-click in Formula Workshop > Formatting Formulas > Group Footer #1 > SumofAmount I get a New Formatting Formulas which has leading zeros as an option, but how do I define this with a Boolean (this is a requirement)?
Right click on the field you need to have leading zeros.
select format field
select the common tab
select X2 to the right of Display String
Enter the following formula
right("000000000000000"&totext(CurrentFieldValue,"#"),15)
This assumes the total length of the field is 15. To change for your required length
change the number 15 to the desired length of the field
change the number of 0's between the quotes to at least the desired
length of the field
Repeat this for any field you need leading zeros

Converting a date/math formula in Excel into Numbers for Mac

I have a formula in Excel that subtracts a birth date from today's date and divides by 365 which gives the age in decimal format. Example below.
B4 is equal to birthday of 10/03/2011.
E4 is today's date.
The result is 2.73. My child is a little over 2 and 1/2.
=IF(B4>0,(E$4-B4)/365," ")
When I try to use this formula in Numbers for Mac, it gives me an error about comparing dates with numbers and so. I looked at DatedIF, TimeValue, and DateValue but couldn't figure out how to do it in Number.
Anyone know how I could get this formula to return a decimal value of 2.73 years of age?
Assuming B4 is in "date" format, you could try the following formula
=IF(ISBLANK(B4),"",YEARFRAC(B4,TODAY(),1))
Here is some documentation on YEARFRAC from the horse's mouth
Try entering the formula
=B4>0
in a different cell, you will then encounter en error
You can’t compare a date with a number because their data types are different.

Fraction issue in Crystal Report

I am facing a strange problem in crystal report 2008
I have a table (mytable) with two columns col1 is string and col2 is float(15)
Below are values in table
Col1 Col2
AA 5.82518987E-5
BB 5.88383009E-5
Created a report in crystal report and placed mytable values on report for col1 it displays correct value but for COL2 it always displays FOR AA "0.0000582519" AND FOR BB "0.0000588383" instead of actual value in the table.
This is what in Crystal report instead of actual value.
Col1 Col2
AA 0.0000582519
BB 0.0000588383
Please help
Thanks
CR has the interesting limitation that the highest precision numeric value it can display is 10 decimal places (this includes trying to use the totext() function to cast the number to a string). This means that any float over that precision will be rounded at that tenth decimal.
Because of this and the fact that you're using a float which is precise up to 15 digits, the quick and dirty solution is to just manually convert to scientific notation by moving your decimal place over 5. You can accomplish this with something like:
totext({table.numeric} * 10^5,10) & "E-5"
Obviously, this will just indiscriminately convert all your numbers in Col2 to scientific notation whether they require it or not, so you may wish to add some additional conditional processing.
Note that if you Google "Crystal Reports scientific notation" you can find some custom functions to do that for you... but just be careful because a lot of these functions do not take the additional precision into account and WILL result in a much less precise number. In fact, the very first hit (HOw to display Number in Scientific Notation in version 2008) will chop that precision down to 10 digits in the very first line by using the totext() function.