SAS IF THEN Statement for Range of Columns - sas

I want to create a new column based on a range of columns(GAP1-GAP5). I wanted to use something like this:
IF FIND(GAP1-GAP5,'New Start') THEN FILTER_NewStart=1
but got an error so had to use this:
IF FIND(GAP1,'New Start') OR FIND(GAP2,'New Start') OR FIND(GAP3,'New Start')
OR FIND(GAP4,'New Start') OR FIND(GAP5,'New Start') THEN FILTER_NewStart=1;
Do I need to use a loop or can I use a function for a range of columns to achieve this?

Try concatenating them and using find on the variable instead.
if(find(cat(of GAP1-GAP5), 'New Start') ) then FILTER_NewStart = 1;

Sounds like you want to use the WHICHC() function to find the first variable in a list of variables that equals a specific value. If none have it then the result is zero.
FILTER_NewStart=0 ne whichc('New Start',of GAP1-GAP5);
FIND() is for locating a specific substring in a longer string and so cannot work on many variables at once.

#Tom whichc() is a good idea, whereas it doesn't work if GAP1-GAP5 just contain but not equal to 'New Start'. find() would be a better anwser in this situation.

Related

How to return a value when a cell matches something without a certain set of characters? Using regexmatch

I'm using IFS and REGEXMATCH to assign values to each cell. The desired outcome is to return 'Info' if a cell contains 'how' 'where' 'what' 'who' but at the same time does not contain 'buy' 'coupon' 'discount' and 'deals'. If the cell contains 'buy' 'coupon' 'discount' and 'deals', it should return 'Commercial'.
For example: cell containing 'where to go' should be Info,
cell containing 'how to buy' should be 'Commercial' because of the 'buy' keyword
My current formula looks like this:
=IFS(REGEXMATCH(J9,"**how|where|what|who**<>buy<>coupon<>discount<>deals"),"Info",REGEXMATCH(J9,"buy|coupon|discount|deals"),"Commercial")
The problem here is it is returning 'Info' for cells like 'how to buy' when it should return 'Commercial'. It only works for 'who', but not for 'how' 'where' and 'what'.
Any thoughts what could be wrong?
I'm still new to regex, would appreciate it if anyone could help me fix this!
You can use the following formula instead
=IF(AND(REGEXMATCH(J8,"how|where|what|who"),NOT(REGEXMATCH(J8,"buy|coupon|discount|deals"))),"Info","Commercial")
You cannot use <> in a regex. We use the NOT instead.
Functions used:
REGEXMATCH
IF
AND
NOT

Assign nested function to variable with parameter

disclaimer: My title may not be accurate as far as what I would like to accomplish, but I can update if someone can correct my terminology
I have 2 functions, each with a separate purpose and usable on its own, but occasionally I would like to combine the two to perform both actions at once and return a single result, and to do this I would like to assign to a variable name
I know I can create a 3rd function that does basically what I want as it is really simple.. though it's become a bit of a challenge to myself to find a way of doing this
def str2bool(string):
return string.lower() in ("yes", "true", "t", "1")
def get_setting(string):
if string == 'cat':
return 'yes'
else:
return 'no'
VALID_BOOL = str2bool(get_setting)
print VALID_BOOL('cat')
So basically I would like to assign the combination of the 2 functions to a variable that I can call and pass in the string parameter to evaluate
In my real world code, get_setting() would retrieve a user setting and return the value, I would then like to test that value and return it as a boolean
Again I know I can just create a 3rd function that would get the value and do the quick test.. but this is more for learning to see if it can be done as I'm trying to do.. and so far my different variations of assigning and calling aren't working, is it even possible or would it turn too complex?
Using lambda is easy, but i don't know if it is exactly what you are looking for.
Example:
f = lambda astring : str2bool(get_setting(astring))
Outputs:
>>> f('cat')
True

Scala. foreach in one element Lists of Dates

I want to create a function that works on a single date or over a period of time. For that I make a list of strings like this:
import com.github.nscala_time.time.Imports._
val fromDate = "2015-10-15".toLocalDate
val toDate = "2015-10-15".toLocalDate
val days = Iterator.iterate(fromDate)(_ + 1.day).takeWhile(_ <= toDate).map(_.toString)
Now I want to access days content:
scala> days.foreach(println)
2015-10-15
scala> days.foreach(day => println(day))
With the first foreach I get the only element in days list, but with the second I get nothing. This is just a sample, but I need to use the 2nd form, because I need to use day value inside the function.
If I use a range both methods work like they are supposed to...
Anyone knows why this behaviour?
Thanks in advance!
P.D. I don't want to use another method to create the list of strings (unless I can't find a solution for this)
Second function works in same way as first.
You've created Iterator object, which you can iterate only once.
To be able use it any amount of times just convert you iterator to list.
val daysL = days.toList

Python loop with condition: using same code whether condition is met or not

I have a dict and a list:
main = {"one": "apple", "two":"pear", "three":"banana", "four":"cherry"}
conditional_list = ["one", "four"]
The conditional list may be empty or contain values (like in the case now). I would like to iterate over the dict "main". BUT: if the conditinal list is not empty, I would like to iterate only over the items that match the conditional list. So:
for key, val in mainlist:
if conditional_list:
if key in conditional_list:
do something
else:
do exactly the same thing
I it possible to set up the iteration in such a way, that I don't have to copy+paste the whole code of "do something" to "else" (the line where it says "do exactly the same thing")? Or to put it in another way: Is there a way to ignore the line "if key in conditional_list" if the condition is NOT met (i.e. the list is empty)?
The thing is, the code "do something" is huge and needs to be updated from time to time, copy+pasting it would make things extremly complicated. Thanks in advance!
What about this:
for key, val in mainlist:
if not conditional_list or key in conditional_list:
do something
My suggestion would be to pass the key to a doSomething() function
Also, you may have a reason, but it looks like the dictionary is being used backwards; given the context, this may be an apt solution for you:
for i in conditional_list:
doSomething(i,d(i),(i in mainlist))

Django Query in a loop fails for no good reason

I have this code:
msgs = int(post['time_in_weeks'])
for i in range(msgs):
tip_msg = Tip.objects.get(week_number=i)
it always results in an error saying that no values could be found.
week_number is an integer field. When I input the value of i directly,
the query works.
When i print out the value of i I get the expected values.
Any input at all would be seriously appreciated.
Thanks.
The range function will give you a zero based list of numbers up to and excluding msgs. I guess there is no Tip with week_number=0.
Also, to limit the number of queries you could do this:
for tip in Tip.objects.filter(week_number__lt=msgs):
#do something
or, if you want specific weeks:
weeks=(1,3,5)
for tip in Tip.objects.filter(week_number__in=weeks):
#do something