Convert number to Date in Qlikview macro - macros

I read a number for example 42000 that I need to convert into date in the format DD/MM/YY in a macro, not a Load statement.
In the macro the number is stored in vInput and the new value will be stored in vDate. Neither
vDatet = Date(vInput, 'DD/MM/YYYY')
or
vDate = date(vInput#(Date,'DD/MM/YYYY'))
works. Any ideas?

You can use the script below to get the content of vInput variable, evaluate the Qlik expression and save the result in the vDate variable.
Using the script below if vInput is equal to 42000 the vDate will be equal to 27/12/2014
sub ChangeDate
set vInputVar = ActiveDocument.Variables("vInput")
vInput = vInputVar.GetContent.String
changedDate = ActiveDocument.Evaluate("=Date(" & vInput & ",'DD/MM/YYYY')")
set vDateVar = ActiveDocument.Variables("vDate")
vDateVar.SetContent changedDate , true
end sub

Related

Suppressing unwanted spaces in dates

From reading code from elsewhere, I have a matrix of dates called 'time' that have unwanted spaces that I want removed.
I've tried isspace and regexprep with no luck
time = regexprep(time, '\W', '');
I have about 130000 dates with the following format:
04-July -2017 09:54:30.000
04-July -2017 09:54:31.000
etc
There are two spaces between the end of 'July' to the next dash I want to suppress to:
04-July-2017 09:54:30.000
04-July-2017 09:54:31.000
Replace two or more spaces with nothing:
>> time = {'04-July -2017 09:54:30.000'
'04-July -2017 09:54:31.000'}
>> regexprep(time,' {2,}','')
{'04-July-2017 09:54:30.000'}
{'04-July-2017 09:54:31.000'}
Unless you just want to correct your input file for later usage, you do not necessarily need to correct the input. There are several ways to parse the time directly with the extra spaces:
Let time be:
time = ['04-July -2017 09:54:31.000';
'04-July -2017 09:54:32.000']
Then to directly parse the string representation of the datetime into a MATLAB date serial number you can use:
%% get date in [MATLAB date serial number]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
matlabTime = datenum(time,formatIn)
matlabTime =
736880.412858796
736880.41287037
This serial time representation is not so human readable but it's the fastest thing you can get if you want to do calculations with date/time.
If your goal is simply to correct the string, then you can use the same trick to read the value in, and define exactly which output format you want out:
%% get date in [string]
formatIn = 'dd-mmm -yyyy HH:MM:SS.FFF' ;
formatOut = 'dd-mmm-yyyy HH:MM:SS.FFF' ;
stringTime = datestr(datenum(time,formatIn),formatOut)
stringTime =
04-Jul-2017 09:54:31.000
04-Jul-2017 09:54:32.000
If you want to use the new datetime objects, the input format has a slight different syntax but the operation is roughly the same:
%% get date in [datetime] objects
formatIn = 'dd-MMM -yyyy HH:mm:ss.SSS' ;
t = datetime(time,'InputFormat',formatIn)
t =
04-Jul-2017 09:54:31
04-Jul-2017 09:54:32
Although the MATLAB console display t in human readable format, t is now a datetime object. Check the documentation if you want to use this.
Replace only two white-spaces after a month and preceding a dash (-):
>> date = '04-July -2017 09:54:30.000';
>> regexprep(date, '(\w) -', '$1-')
ans =
'04-July-2017 09:54:30.000'

Add the date of creation to a filename in SystemVerilog

I need to create a report(.txt) and I want to reference each sessions of tests, so I wanted that for each simulations, I add the date to name of my report.
Like Report_01-19-2017-12:53.txt
So far I have been able to create either a file with the date inside with :
$system("date > sDate");
or display it on my simulation software with :
$system("date");
So far,My code look like :
string filename;
reg [8*30:1] data; // the date is of 29 characters in size
string sDate;
integer scan_file,data_file ;
initial begin
$system("date > data");
data_file = $fopen("data", "r");
scan_file = $fscanf(sDate,"%s", data);
$fclose("data");
[...]
filename = {filename,sDate};
Report_Task = $fopen(filename,"a"); [...]
end
sDate contains nothing, date contains the date...
I tried string and reg for filename and sDate
Instead of going through files to get the date, you could use svlib. Chapter 9 of the documentation illustrates how to get information about the current time.
Don't you mean
scan_file = $fscanf(data_file, "%s", sDate);
where, if the read is successful, scan_file will be equal to 1 (the number of items read). (So, you probably didn't want to call it scan_file.)

SAS - Fill in null values for numeric variables but not dates

I use the following to fill in null values for numeric variables with 0, but this does it for date variables as well. How can I fill in null values only for non-date numeric variables?
data mydataset;
set mydataset;
array myarray _numeric_;
do over myarray;
if myarray=. then myarray=0;
end;
run;
There is an excellent post on SAS Communities about determining if a variable is a date or not. It's available here. Your question isn't trivial, as you shouldn't forget about user-defined formats which can also behave like date formats (proc format). Let's suppose that all your date variables are of one format.
data test;
format x y z datetime8.;
x = .;
y = .;
z = .;
run;
%macro get_vars_format(lib, tab, fmt);
proc sql noprint;
select name into :names separated by ' '
from sashelp.vcolumn
where libname = "&lib." and
memname = "&tab." and
format eq "&fmt.";
quit;
%put Names: &names.;
data work.test;
set &lib..&tab.;
%let i = 1;
%let name = %scan(&names., &i., %str( ));
%do %while(&name. ne );
if &name. eq . then &name = 0;
output;
%let i = %eval(&i + 1);
%let name = %scan(&names., &i., %str( ));
%end;
run;
%mend get_vars_format;
%get_vars_format(lib=WORK, tab=TEST, fmt=DATETIME8.);
This macro takes three arguments:'
library name,
data set name,
and desired format name.
I am saving all the variables names into a macro-variable (they are separated by a space sign) and in a next step, I am iterating this in a loop (note i and name macro-variables). For every row, if a value of any variable with a given format is equal to . then replace it with 0. Note that if a variable has a date format, 0 will be treated as a Jan 01, 1960, as that's the first day in SAS date value convention.

Libreoffice calc macro to get current date + 14 days into a cell

I need to get todays date + 14 days string formatted in standard dd.mm.yyyy format into Libre Office calc cell.
I already tried the code below, but I lack the knowledge to cope with "Object variable not set" error.
REM ***** BASIC *****
sub Datumplus14
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
Dim cell as object
dim term as date
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
term = today()
cell.String = DateAdd("d", 14, datum)
dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, cell)
end sub
Different ideas on how to get this done instead of macro is welcome as well.
Hmm, not sure if this will answer the specifics of what you're actually trying to do, but you can easily get the current date and just +14 to it directly in a cell formula.
Like so:
=NOW()+14
The rest is just applying the required date format to that cell. You can also grab the date from another cell too.
Function myFunction() As String
myFunction = Format(Now()+14, "dd.mm.yyyy")
End Function

Data validator VBA excel- compare a value within a string in one cell to a value in another

Have not been able to find anything that fits my needs.
I have two columns of values (L and U). Column L contains a file names that includes a date in MM-DD-YYYY format (example yadayadayada thru (03-15-2015).pdf) column U contains a date. What I need to do is have a macro compare the date within the file name to the date in the column U. Other dates may appear within the text in column L but the date I need to compare against is always after "thru" and in parentheses followed by the file extension. If they do not match, I need the value in column U highlighted and replaced with the text "FAIL". I'm going to continue searching but any help is greatly appreciated. Thanks!
Does it have to be VBA? This can be accomplished with Conditional Formatting.
Apply this conditional format formula to column U:
=AND(U1<>"",L1<>"",U1<>--TRIM(MID(SUBSTITUTE(TRIM(RIGHT(SUBSTITUTE(L1," ",REPT(" ",255)),255)),")",REPT(" ",255)),2,255)))
And set the number format to Custom "FAIL" with yellow (or the highlight color of your choice) fill.
EDIT
If it has to be VBA, then this should work for you:
Sub tgr()
Const HeaderRow As Long = 1 'Change to your actual header row
Dim ws As Worksheet
Dim rngFail As Range
Dim rngFiles As Range
Dim FileCell As Range
Dim dDate As Double
Set ws = ActiveWorkbook.ActiveSheet
Set rngFiles = ws.Range("L" & HeaderRow + 1, ws.Cells(Rows.Count, "L").End(xlUp))
If rngFiles.Row < HeaderRow + 1 Then Exit Sub 'No data
For Each FileCell In rngFiles.Cells
If Len(Trim(FileCell.Text)) > 0 Then
dDate = 0
On Error Resume Next
dDate = CDbl(CDate(Trim(Mid(Replace(Trim(Right(Replace(FileCell.Text, " ", String(255, " ")), 255)), ")", String(255, " ")), 2, 255))))
On Error GoTo 0
If dDate <> ws.Cells(FileCell.Row, "U").Value2 Then
Select Case (rngFail Is Nothing)
Case True: Set rngFail = ws.Cells(FileCell.Row, "U")
Case Else: Set rngFail = Union(rngFail, ws.Cells(FileCell.Row, "U"))
End Select
End If
End If
Next FileCell
If Not rngFail Is Nothing Then
rngFail.Value = "FAIL"
rngFail.Interior.ColorIndex = 6
End If
End Sub