Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65570

Using a formatted date string in a macro

$
0
0
I have a dataset that is tsset, and I would like to use the formatted (string) value of the date variable, from one particular observation, in generating a totally new variable name. For example, if the 6th observation is 1987q3, I would like a new variable named var1987q3. Here is what I have tried

Code:
webuse gdpoil

gen var`=tq(qdate[6])' =5     // macro is blank
gen var`tq(qdate[6])' =5      // macro is blank

levelsof qdate if _n==6, local(a)
gen var`a' = 5     // kind of works but macro resolves to the numeric date value, not the formatted date string


gen mystr = string(qdate, "%tq")
levelsof mystr if _n==6, local(a)
gen var`a' = 5     // error message "1987q3 invalid name
Thank you for your help.

Viewing all articles
Browse latest Browse all 65570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>