Skip to content

Instantly share code, notes, and snippets.

@michaelstepner
Last active September 22, 2023 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelstepner/7026967a50db98bd39a58b20f01dbe70 to your computer and use it in GitHub Desktop.
Save michaelstepner/7026967a50db98bd39a58b20f01dbe70 to your computer and use it in GitHub Desktop.
A Stata program to output a number or string to YAML
v 0.0.2
d Michael Stepner, stepner@mit.edu
p 'yamlout': export key, value, and comment to a YAML file
*! version 0.0.2 19may2017 Michael Stepner, stepner@mit.edu
program define yamlout
syntax using, [key(string) VALue(string) comment(string) fmt(string) replace]
* Check for valid combination of key, value, comment
if (`"`key'`value'`comment'"'=="") {
di as error "key(), value() and comment() cannot all be blank"
exit 198
}
if ("`value'"!="") & ("`key'"=="") {
di as error "must specify key() if specified value()"
exit 198
}
* Format value
if (real("`value'")!=.) | ("`value'"==".") { // numeric value
if ("`fmt'"!="") local value=string(`value',"`fmt'")
else local value=string(`value')
}
else { // string value
if ("`fmt'"!="") {
di as error "can only specify fmt() for numeric value"
exit 198
}
}
* Replace or Append?
if ("`replace'"=="replace") local overwrite replace
else local overwrite append
* Open file
tempname file
file open `file' `using', write text `overwrite'
* Write YAML line
if (`"`value'`key'"'=="") file write `file' `"# `comment'"' _n
else if (`"`comment'"'=="") file write `file' `"`key': '`value''"' _n
else file write `file' `"`key': '`value'' # `comment'"' _n
* Close file
file close `file'
end
v 0.0.2
d
d 'yamlout': export key, value, and comment to a YAML file
d
d Distribution-Date: 20170519
d
f yamlout.ado
f yamlout.sthlp
{smcl}
{* *! version 0.0.2 19may2017}{...}
{viewerjumpto "Syntax" "yamlout##syntax"}{...}
{viewerjumpto "Description" "yamlout##description"}{...}
{viewerjumpto "Options" "yamlout##options"}{...}
{viewerjumpto "Examples" "yamlout##examples"}{...}
{viewerjumpto "Author" "yamlout##author"}{...}
{title:Title}
{p2colset 9 14 21 2}{...}
{p2col :{hi:yamlout} {hline 2} Export key, value, and comment to a YAML file}{p_end}
{p2colreset}{...}
{marker syntax}{title:Syntax}
{p 8 15 2}
{cmd:yamlout} using filename, [{cmd:key(}{it:string}{cmd:)}] [{cmd:value(}{it:string}{cmd:)}]
[{cmd:comment(}{it:string}{cmd:)}] [{cmd:fmt(}{it:string}{cmd:)}]
[{cmd:replace}]
{synoptset 40}{...}
{marker options}{title:Options}
{synopthdr :Option}
{synoptline}
{synopt :{cmdab:key(}{it:string}{cmd:)}}specify the key for the YAML line{p_end}
{synopt :{cmdab:value(}{it:string}{cmd:)}}specify the value for the YAML line{p_end}
{synopt :{cmdab:comment(}{it:string}{cmd:)}}provide a comment for the YAML line{p_end}
{synopt :{cmdab:fmt(}{it:string}{cmd:)}}formatting for numeric values{p_end}
{synopt :{cmd:replace}}replace the existing file, if not specified the content will be appended{p_end}
{synoptline}
{p2colreset}{...}
{marker description}{title:Description}
{pstd}
The {cmd:yamlout} command exports a single line in YAML format containing the specified key, value, and optional comment to a given file.
{p_end}
{pstd}
If the file exists and the {cmd:replace} option is not used, it will append to the file. If the {cmd:replace} option is used, it will overwrite the existing file.
{p_end}
{marker examples}{title:Examples}
{pstd}
{cmd:. yamlout} using myfile.yaml, key(age) value(25) comment(This is an age)
{p_end}
{pstd}
{cmd:. yamlout} using myfile.yaml, key(name) value(John)
{p_end}
{marker author}{...}
{title:Author}
{pstd}Michael Stepner{p_end}
{pstd}stepner@mit.edu{p_end}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment