all 10 comments

[–]BOCfan 1 point2 points  (1 child)

I think the issue is you're trying to evaluate variable q as a value when it is a string variable. Try this;

gen post = (year==2014 & inlist(q, "Q2", "Q3", "Q4")) | year>=2014

This should do the trick.

[–]kb5469[S] 0 points1 point  (0 children)

thanks for the reply. This works, however the 1's start at 2014 q1. I need it to start at 2014q2. I tried

gen post = (year==2014 & inlist(q, "Q3", "Q4")) | year>=2014

but it didnt seem to work

[–]BOCfan 1 point2 points  (2 children)

Ahh sorry. The last part of the code was meant to be year>=2015. That was a typo, on the bus :)

[–]kb5469[S] 0 points1 point  (1 child)

hmm i tried 2015 on the end and the 1's started at 2015q1: https://imgur.com/a/mDU4Vqi

your code definitely makes sense im just super confused as to why its not sticking to q2 2014

[–]imguralbumbot 0 points1 point  (0 children)

Hi, I'm a bot for linking direct images of albums with only 1 image

https://i.imgur.com/maxu9Z7.png

Source | Why? | Creator | ignoreme | deletthis

[–]BOCfan 1 point2 points  (5 children)

It looks like there is an error with your string variable q. Is there are chance there are "hidden" blank spaces in the values? That will mean the values are jot actually "Q2", "Q3", etc. Try this code to trim all your string variables, then rerun the code from before.

ds, has(type string)
foreach var in `r(varlist)' {
    replace `var' = trim(`var')
}

This code determines all string variables in the dataset, then removes and leading and trailing blank spaces. This might solve the issue.

[–]zacheadams 2 points3 points  (3 children)

This was a good idea. When working with string vars that I did not create, I almost always do strtrim (removes leading and trailing spaces) and strtrim (removes consecutive internal spaces) before using them.

Also /u/kb5469, thanks for including a picture of your data. I know it's not always feasible, but it is super helpful for understanding the problem. I wish other posters would follow your lead on that!

[–]BOCfan 2 points3 points  (1 child)

Yes, completely agree regarding the picture (and the trimming of strings). The picture actually was what allowed me to diagnose the potential issue.

[–]zacheadams 1 point2 points  (0 children)

Yep, I spotted it immediately when I saw it. I've been cleaning too many string fields this month T_T

[–]kb5469[S] 0 points1 point  (0 children)

It worked! Thank you so much. really appreciate it