How do I write a macro in Stata to identify the file with most recent date in title? - date

I am working with a set of .dta files in Stata, each of which takes some time to create and each of which contains the date of creation in the file name (created at the point of saving using a macro with today's date).
At the moment my do-files identify the relevant .dta file to open based on the today's date macro, but this requires that I run the code to create the .dta files each day.
Is there a way of asking Stata to identify the most recently dated file from a set of files with same filename stem and different dates within a folder (and then open it), once I have run the "cd" command? I had a look on Statalist and SO but couldn't see an answer - any advice gratefully received.
e.g. In the folder, I have files 2020-08-23_datasetA.dta, 2020-08-22_datasetA.dta, 2020-08-22_datasetB.dta etc, and at different points I will want to select the most recently-dated version of A, B, C etc. Hence don't think a simple sort will work as there are datasets A, B, C at play.
(My question is essentially the Stata version of this one about R - Loading files with the most recent date in R)
[edited to clarify that there are multiple datasets, each of which is dated and each of which will need to be opened at different points]

Manifestly two or more files in a particular folder can't have the same name. But we know what you mean.
A utility like fs from SSC will return a list of filenames matching a pattern, alphanumerically sorted. With your dating convention the last named will be the latest as your dates follow (year, month, day) order.
Using another convention for the rest of the filename won't undermine that, but naturally you need to spell out which subset of files is of interest. So a pattern is
. ssc install fs
. fs *datasetA.dta
. local wanted = word(r(files), -1)
where the installation need only take place once. You can circumvent fs by using the calls to official Stata that it uses.
Perhaps you are seeking a program, but it's not clear to me that you need one.
Small detail: You're using the word macro in a way that doesn't match its Stata sense. Stata, for example, is not SAS! The terms code, routine and script are generic and don't conflict with any Stata use. Conversely, code, routine or script may have fixed meanings in other software you use. Either way, Stata questions are best phrased using Stata terms.

Related

Converting WordPerfect files with mail-merge logic to .docx

I am working with a large number of .frm and .wpd WordPerfect files and I am trying to convert them to .docx while retaining all of the mail-merge logic.
Saving the files as .docx within WordPerfect X6 or opening the .frm/.wpd files from within Word 2016 both result in a complete loss of the mail-merge codes.
Is there any way to convert the WordPerfect files so that they retain similar merge functionality in the .docx format?
Here is a somewhat crazy approach:
Use WP_Reader to parse the document.
Collect all the pertinent merge functions (there are a bunch of merge commands, and you will have to figure out which ones you need to translate into Word merge fields).
Make a dictionary that translates each needed merge function into its WP string "code" (for searching purposes).
Write a simple WordPerfect macro that uses a passed-in variable to search and replace each merge "code" with a unique text marker.
Open the document using the WordPerfect.PerfectScript interface.
Use the PerfectFit.PerfectScript interface to call the WP macro and pass in each merge function you want replaced with its unique marker. You can't make a .NET call to a WordPerfect document to search for anything that is not ASCII text, so you are forced to use a macro and PerfectFit to do this (thanks, Corel!).
Save the wpd document as a Word document (or probably safer, use Office Interop to do this).
Use the Office Interop commands to find each unique marker and replace it with a new merge field (I'm a little fuzzy about how this part works).
This approach comes with a few caveats:
WP_Reader in its current form does not model any merge functions. This will take a bit of coding, but the approach is pretty straightforward. I'm the only person working on this project, and have not yet had a need for these functions. All of the merge functions either have no parameters or 1 or more strings. These functions should be easy to model, and I can help you get started.
.frm and .dat files are really WordPerfect files that are set up as merge documents and data sources for merge documents (maybe they are used for other purposes, too). You can use WP_Reader to model those files the same as .wpd files.
This approach will only work if you have extremely simple merge documents. The WordPerfect merge arena includes its own language, separate from the WP command set. There are If statements, For loops, etc, that would not translate into anything useful in a Word document.
Before you try this time-consuming project, see if LibreOffice converts wpd files with merge codes directly into the .odt format. The lib-wpd project, which is a built-in program in LibreOffice, has been running since 2003 or so, and they might have already incorporated this functionality. Converting from .odt to .docx should be easy to do.

(sas) concatenate multiple files from different folders

I'm a relatively new SAS user, so please bear with me!
I have 63 folders that each contain a uniquely named xls file, all containing the same variables in the same order. I need to concatenate them into a single file. I would post some of the code I've tried but, trust me, it's all gone horribly awry and is totally useless. Below is the basic library structure in a libname statement, though:
`libname JC 'W:\JCs\JC Analyses 2016-2017\JC Data 2016-2017\2 - Received from JCs\&jcname.\2016_&jcname..xls`
(there are 63 unique &jcname values)
Any ideas?
Thanks in advance!!!
This is a common requirement, but it requires a fairly uncommon knowledge of multiple SAS functions to execute well.
I like to approach this problem with a two step solution:
Get a list of filenames
Process each filename in a loop
While you can process each filename as you read it, it's a lot easier to debug and maintain code that separates these steps.
Step 1: Read filenames
I think the best way to get a list of filenames is to use dread() to read
directory entries into a dataset as follows:
filename myfiles 'c:\myfolder';
data filenames (keep=filename);
dir = dopen('myfiles');
do file = 1 to dnum(dir);
filename = dread(dir,file);
output;
end;
rc = dclose(dir);
run;
After this step you can verify that the correct filenames have been read be printing the dataset. You could also modify the code to only output certain types of files. I leave this as an exercise for the reader.
Step 2: use the files
Given a list of names in a dataset, I prefer to use call execute() inside a data step to process each file.
data _null_;
set filenames;
call execute('%import('||filename||')');
run;
I haven't included a macro to read in the Excel files and concatenate the dataset (partly because I don't have a suitable list of Excel files to test, but also because it's a situational problem). The stub macro below just outputs the filenames to the log, to verify that it's running:
%macro import(filename);
/* This is a dummy macro. Here is where you would do something with the file */
%put &filename;
%mend;
Notes:
Arguably there are many are many examples of how to do this in multiple places on the web, e.g.:
this SAS knowledge base article (http://support.sas.com/kb/41/880.html)
or this paper from SUGI,
However, most of them rely on the use of pipe to run a dir or ls command, which I feel is the wrong approach because it's platform dependent and in many modern environments the ability to pipe shell commands will be disabled.
I based this on an answer by Daniel Santos in communities.sas.com, but, given the superior functionality of stackoverflow I'd much rather see a good answer here.

Bulk change date to present date with ExifTool

I have a bunch of images with different create dates. I want to normalize them all to a given date (say today's date) using a batch file (Windows). Can ExifTool set dates? I only see documentation and examples for shifting dates. To shift the date to present, I would need to somehow read the date for each file, calculate the difference, and then shift. That would be ok, but I don't know how to read the create date into a variable using ExifTool.
One obstacle for me is that I don't speak Perl. I do Python, and there is pyexiv2. This allows to write the "date", but I can't see anywhere if that is just create date or all dates.
Edit
Here shows using, for example,
exiftool -AllDates='2010:08:08 15:35:33' -overwrite_original IMG_01.jpg
in Ubuntu linux, but I could not get that to work in Windows.
As a hack, I tried
exiftool -AllDates+=2015:03:02 IMG_8220.JPG
which set the dates to the time executed, probably because the shift was so completely crazy. But I'd like to have control, and, specifically be able to change YYYY:MM:DD without changing the time.
Consequently, help still appreciated.
I crossposted to the ExifTool forum, and ExifTool author Phil Harvey responded that you need to use double quotes. The single quotes from the linked blog post don't do it in Windows.
So, one would use
exiftool -AllDates="2010:08:08 15:35:33" IMG_01.jpg
I tested it and (of course) it worked.

LaTeX command for last modified

Is there a LaTeX command that prints the "last modified" date of the actual document? Since LaTeX projects consist of more than one file this command ideally prints the date of the actual file, not that of the project.
pdfTeX provides the primitive \pdffilemoddate to query this information for files. (LuaTeX uses its own Lua functions for the same thing.) Since pdfTeX is used by default in all LaTeX distributions in the last few years (at least), there's no harm in using the new functionality unless you're dealing with very old production systems. Here's an example:
\documentclass{article}
\begin{document}
\def\parsedate #1:20#2#3#4#5#6#7#8\empty{20#2#3/#4#5/#6#7}
\def\moddate#1{\expandafter\parsedate\pdffilemoddate{#1}\empty}
this is the moddate: \moddate{\jobname.tex}
\end{document}
(Assuming the file has been modified since year 2000.)
The package filemod seems to do exactly what you need. To get the last modified date of the file you just include the package in the usual way:
\usepackage{filemod}
and the modification time of the current document is printed by:
\filemodprintdate{\jobname}
you can also print the modification time, and there are many options to format the output.
Unfortunately, TeX does not provide commands for such information; the only way to get such information is
by running a non-TeX script to create a TeX file before running LaTeX and including this file in your main LaTeX document somehow, or
by running the external script from TeX (which only works if the so-called write18 or shellescape feature is enabled; you'd have to consult the manual of your TeX implementation for this, and not have a stubborn sysadmin).
It is possible that extended TeXs do support file info commands (luaTeX perhaps?), but it's not part of TeX proper.
If you are using an automated build system, you could ask it to generate a file (perhaps named today.sty) which depends on all the source files.
In make that might look like:
today.sty: $LATEX_SRCS
echo "\date{" > $#
date +D >> $#
echo "}" >> $#
and \usepackage{today.sty}.
The will use the date of the first build after a file changes, and won't update until either you delete today.sty or alter another source file.
thank dmckee
LATEX_SRCS = test.tex
define moddate
date +%Y%m%d%H%M%S
endef
today.sty: $(LATEX_SRCS)
#echo "\def\moddate{"$(shell $(moddate))"}"> $#
There is the getfiledate LaTeX package (it was part of my LaTeX distribution by default). It seems to be designed to automatically output a paragraph like:
The date of last modification of file misc-test1.tex was 2009-10-11  21:45:50.
with a bit of ability to tweak the output. You can definitely get just the date. However, I couldn't figure out how to get rid of newlines around the date and how to change the date format. To be honest I think the authors implemented it exactly for the single purpose they needed it, and it is rather cumbersome for general use.

smlnj rephrased question for listdir(filename, directoryname)

i am a newbie learning sml and the question i am thrown with involves IO functions that i have no idea how it works even after reading it. Here is the 2 questions that i really need help with to get me started, please provide me with codings and some explaination, i will be able to trial and error with the code given for the other questions.
Q1) listdir(filename,directoryname), which given the name of a directory, list its contents in a text file. The listing is in a form that makes it easy to seperate filenames, dates and sizes from each other. (similar to what msdos does with "dir" but instead of just listing it out, it places all the files and details into a text file.
Q2) readlist(filename) which reads a list of filenames (each of which were produced by listdir in (Q1) and combines them into one large list. (reads from the text file in Q1 and then assigning the contents into 1 big list containing all the information)
Thing is, i only learned from the lecturer in school on the introduction section, there isnt even a system input or output example shown, not even the "use file" function is taught. if anyone that knows sml sees this, please help. Thanks to anyone who took the effort helping me.
Thanks for the reply, current I am using SMLNJ to try and do this. Basically, Q1 requires me to list the directory's files of the "directoryname" provided into a text file in "filename". The Q2 requires me to read from the "filename" text file and then place the contents into one large list.
Duplicate of: smlnj listdir
As a hint I will say that you have to make use of these functions:
OS.FileSys.OpenDir(directoryname) - this will open directory stream for you (Q1)
TextIO.openOut(filename) - this will open the file stream (Q2)
TextIO.openIn(filename)- this will open the file (Q2)
If you are stuck and dont' know how to do the progs then I will post the full code here, but i suggest you first give a try.
zubair sheikh