How to loop through a folder with conditional statement in Stata? - for-loop

I have a folder with a bunch of csv files and I want to loop through each file, check if a list of variables in each of the files = 0, then save the csv file as a .dta if they do not equal 0.
I'm having trouble finding online help to do this but here's what I've tried so far:
foreach file in `files' {
import delimited using `file', clear
if a & b & c & d != 0
save "Desktop\myfolder\`file'.dta"
}
when I try this though Stata tells me "{ required r(100)".
Any help appreciated. Thanks.

Stealing some code from the estimable #Wouter Wakker, let's first suppose that the criterion is that a non-zero value is found somewhere in a b c d
foreach file in `files' {
import delimited using `file', clear
local OK = 0
quietly foreach v in a b c d {
count if `v' != 0
if r(N) > 0 local OK = 1
}
if `OK' save "Desktop/myfolder/`file'.dta"
}
Whatever your precise criterion, I think you need to loop over a b c d and (say) count or summarize according to what you want or do not want.

From help ifcmd:
Syntax
if exp { or if exp single_command
multiple_commands
}
So you can do either
foreach file in `files' {
import delimited using `file', clear
if a & b & c & d != 0 save "Desktop\myfolder\`file'.dta"
}
or
foreach file in `files' {
import delimited using `file', clear
if a & b & c & d != 0 {
save "Desktop\myfolder\`file'.dta"
}
}
However, I don't think your if condition does what you think it does. What you're looking for would rather be:
if a != 0 & b != 0 & c != 0 & d != 0

Related

JsqlParser how to rewrite a expression

I have a sql where cluase:
a > 1 and b < 1 and c = 3
In this case I want to remove the a>1, how could I rewrite it to
1 = 1 and b < 1 and c =3
I have try the ExpressionVisitor, but couldn't rewrite the Expression
You can simply replace the where part of a SQL by using something like:
((PlainSelect)selectBody).setWhere(CCJSqlParserUtil.parseCondExpression("mycol = 10"));
but if you want to use the where expression itself and want to find in it the expression a > 1, then you have to use something like
Select stmt = (Select) CCJSqlParserUtil.parse("select * from a where a > 1 and b < 1 and c = 3");
System.out.println("before " + stmt.toString());
((PlainSelect) stmt.getSelectBody()).getWhere().accept(new ExpressionVisitorAdapter() {
#Override
public void visitBinaryExpression(BinaryExpression expr) {
if ("a > 1".equals(expr.getLeftExpression().toString())) {
try {
expr.setLeftExpression(CCJSqlParserUtil.parseCondExpression("1=1"));
} catch (JSQLParserException ex) {
Logger.getLogger(SimpleSqlParser39ReplaceExpressionInWhere.class.getName()).log(Level.SEVERE, null, ex);
}
}
super.visitBinaryExpression(expr);
}
});
System.out.println("after " + stmt.toString());
This is not a complete solution since you have to check the right expression as well. Since you are indeed not able to modify the binary expression a > 1 itself you have to look for the enclosing expression and replace it there. So that's the main idea.
By the way this is the output of this little snippet:
before SELECT * FROM a WHERE a > 1 AND b < 1 AND c = 3
after SELECT * FROM a WHERE 1 = 1 AND b < 1 AND c = 3

In Go - anything like a NOP (assembler) command?

When debugging i often need to put a conditional breakpoint, ex :
if messageType == 1 {
nop; // <-- breakpoint
}
There is no 'nop' command and am fairly used to just put a dummy line in which is doing, ex :
if messageType == 1 {
x = 1; // <-- breakpoint
}
And then put the breakpoint at the line with x = 1.
Issue is in Go the compiler often starts battling me because im not using x etc - and am just wondering if theres a quick and smarter way of doing it with a oneline statement ? what do you guys do ?
Just don't store it in a real variable:
if messageType == 1 {
_ = 1
}

Shortcut assignment in go excluding empty/nil value

This can be done in python / javascript:
# Python:
a = ""
b = "test"
c = a or b # test
// javascript
a = "";
b = "test";
c = a || b; // test
Can the same be accomplished in go without doing some conditional block? The only way I'm able to do this so far is with conditional blocks...
a := ""
b := "test"
var c
if a {
c = a
} else {
c = b
}
I think this is answer is going to be "no, this cannot be done", but I figured I would ask just in case I'm wrong. The example here is simplified. The variables a and b could have been defined long ago...
From golang FAQ, There is no ternary testing operation in Go. You may use the following to achieve the same result:
if expr {
n = trueVal
} else {
n = falseVal
}

c++ How to check string parsing order

I am a beginner in c++. I have multi line input from command line like below.
A {1,2,3,4}
B {2,1,4,9}
c {5,8,7,6}
My aim is to read each line and to do some action based on the first letter in line. I have a code like below.
while(getline(cin,input)){
stringstream ss(input);
string letter;
while(!ss.eof()){
ss >> letter;
if (letter == "A")
{
Do this
}
if (letter == "B")
{
Do this
}
if (letter== "C")
{
Do this
}
}
}
Now I want to ensure that after A, only B has to be given as input and not A again or C. After B and C input can be in any order. Is there any easy way to check the order of input?

How to remove line `v u` from a file when line `u v` already exists using unix command

I have the following test data :
a b
a c
b a
b c
b d
c a
c b
c d
d b
d c
and I want to remove lines v u when line u v already exists using unix command. For example here I want to obtain :
a b
a c
b c
b d
c d
I've tried with an awk script but on a long file it takes too much time :
{
if(NR==1){
n1=$1
n2=$2
test=0
k=0
i = 0
column1[i]=$1
column2[i]=$2
printf "%s %s\n", column1[i], column2[i]
}
else{
for(k=0; k<=i;k++){
if(column1[k]==$2){
test=1
tmp=i
break
}
}
if(test==1){
if(column2[tmp]==$1){
n1=$1
n2=$2
}
}
else if(n1!=$1||n2!=$2){
n1=$1
n2=$2
i++
column1[i]=$1
column2[i]=$2
printf "%s %s\n", column1[i], column2[i]
}
test=0
}
}
Does someone have an idea ?
I think this can be achieved pretty simply:
awk '!seen[$1,$2]++ && !seen[$2,$1]' file
This only prints lines (the default action) when the first and second column have not yet been seen in either order.
The array seen keeps track of every pair of fields by setting a key containing the first and second field. The expression !seen[key]++ is only true the first time that a specific key is tested because the value in the array is incremented each time.

Resources