SAS EG Character Functions - string

I have two tables containing characters:
First_Column Second Column
aaa 123aaa123
bbb cdsbbbsxd
ccc 098fdsccd
I want to label 1 if Second Column string contains the string in the first column otherwise I would like put 0.
I could not find a way to do that in SAS EG? Is there any function to do this?
Thanks

You can use functions like find, index or count.
count('Second Column'n, First_Column)
index('Second Column'n, First_Column)
find('Second Column'n, First_Column)
In Query Builder you have to add new column with an expression like below:
case(count('Second Column'n, First_Column))
when(0) then 0
else 1
end

Related

Rearrange excel table cells - reordering

I don't know excel very well and I am trying to take something like this (with a lot of entries):
Field ......Value ....... ID
A .......... blabla1 .......1
B ...........blabla2 .......1
C ...........blabla3 .......1
D ...........blabla4 .......1
A ...........blabla5 .......2
B ...........blabla6 .......2
C ...........blabla7 .......2
D ...........blabla8 .......2
and turn into something more readable like this:
ID -----A -------------B ---------------- C ---------------- D
1 ------blabla1 -----blabla2 -------- blabla3 --------blabla4
2 ------blabla5----- blabla6 -------- blabla7-------- blabla8
Does anyone know a good way to do that? Thank you
(sorry about the bad formatting)
The exact delimiter beween each word is key if text not already split in separate cells..
Assuming there are numerous words in place of '.....', with each word separated by a single space (different delimiter would be required if the blablas represented sentences comprising one / more spaces), then you could achieve the desired table representation as follows
(several function in this soln requires Office 365 compatible version of Excel,
the lookup in step 3 does not require Office 365, but may mean IDs and Fields need to be manually entered or VB could be deployed):
Starting position (after removing bank rows):
Field Value ID
A blabla1 1
B blabla2 1
C blabla3 1
D blabla4 1
A blabla5 2
B blabla6 2
C blabla7 2
D blabla8 2
1) Split cells according to delimiter (skip this step if not relevant)
=TRANSPOSE(FILTERXML("<x><y>"&SUBSTITUTE(F3," ","</y><y>")&"</y></x>","//y"))
(replace the " " inside the substitute function with a different delimiter if required/desired)
2) Obtain unique IDs (rows) and Fields (columns)
=UNIQUE(K4:K11)
=TRANSPOSE(UNIQUE(I4:I11))
3) Index lookup for table content
=INDEX(J4:J11,MATCH(M4#&N3#,K4:K11&I4:I11,0),0)

lookup value based on partial value

I have a set of batch numbers in a sheet which are alphanumeric code as follows
sdc234
fgh345
ght587
jki876
The alphabets of the batch number represent a product code. For example
sdc = 20499999
fgh = 45999999
ght = 67999992
jki = 56700000
The above relation is in another sheets.
I want to match product code with batch number directly. How do i lookup a product code based on this partial info ?
You can sort your second table in an alphabetical order and use VLOOKUP with TRUE (approximate match) as your third argument.
Assuming the second table is in column A and B:
D E
sdc234 =VLOOKUP(D1,A:B,2,TRUE)
fgh345 =VLOOKUP(D2,A:B,2,TRUE)
ght587 =VLOOKUP(D3,A:B,2,TRUE)
jki876 =VLOOKUP(D4,A:B,2,TRUE)
The output is as below:
D E
sdc234 20499999
fgh345 45999999
ght587 67999992
jki876 56700000
EDIT:
Assuming your product code is always 3 letters, you can use the LEFT function to get the first 3 letters and then use that as the lookup value. This way you can use the exact match as your third argument:
sdc234 =VLOOKUP(LEFT(D1,3),A:B,2,FALSE)
fgh345 =VLOOKUP(LEFT(D2,3),A:B,2,FALSE)
ght587 =VLOOKUP(LEFT(D3,3),A:B,2,FALSE)
jki876 =VLOOKUP(LEFT(D4,3),A:B,2,FALSE)
Credits to Mladen Savic's comment for making me think of this solution.

Using tbl.Lookup to match just part of a column value

This question relates to the Schematiq add-in for Microsoft Excel.
Using =tbl.Lookup(table, columnsToSearch, valuesToFind, resultColumn, [defaultValue]) the values in the valuesToFind column have a consistent 3 characters to the left and then varying characters after (e.g. 908-123456 or 908-321654 - i.e. 908 is always consistent)
How can I tell the function to lookup the value based on the first 3 characters only? The expected answer should be the sum of the results of the above, i.e. 500 + 300 = 800
tbl.Lookup() works by looking for an exact match - this helps ensure it's fast but in this case it means you need an extra step to calculate a column of lookup values, something like this:
A2: =tbl.CalculateColumn(A1, "code", "x => LEFT(x, 3)", "startOfCode")
This will give you a new column that you can use for the columnsToSearch argument, however tbl.Lookup() also looks for just one match - it doesn't know how to combine values together if there is more than one matching row in the table, so I think you also need one more step to group your table by the first 3 chars of the code, like this:
A3: =tbl.Group(A2, "startOfCode", "amount")
Because tbl.Group() adds values together by default, this will give you a table with a row for each distinct value of startOfCode and the subtotal of amount for each of those values. Finally, you can do the lookup exactly as you requested, which for your input table will return 800:
A4: =tbl.Lookup(A3, "startOfCode", "908", "amount")

Excel Find And Replace Text in Formula

I have 3 columns: A and B contain numbers, and C has a html link.
I want to replace numbers in the html link with the numbers from columns A & B.
Can anyone tell me how I can do this? I tried using the replace function, but it doesnt work for multiple variables.
Example:
random.com/do?ID=1111&SourceID=2222
I'd replace 1111 with ID columan A 617 and 2222 with sourceID column B 2
SourceID LocationID link
617 2 random.com/do?ID=1111&SourceID=2222
1878 39 random.com/do?ID=1111&SourceID=2222
4148 48 random.com/do?ID=1111&SourceID=2222
I would not use the replace formula here because it requires a "position and length" which will change once you update the link with a new value from SourceID or LocationID and the logic to figure out the new location is overkill and not needed.
Instead you can use the Substitute formula instead which I think is more readable and easier to implement. This formula replaces "text" with "text"
If you use 2 Substitutes (1 outer and 1 nested) you can replace both strings in one line. Try putting this in column D
=SUBSTITUTE(SUBSTITUTE(C1,"1111",A1),"2222",B1)
and you can auto-fill down

Excel - Looping through data till it finds the right one

I'm pulling data from one Excel table into another using an IF statement. I want it to check two fields and if it is a match I want it to print something and if it doesn't then I want it to continue searching. If there is no absolute match then leave the field blank.
I believe I'm running into an syntax issue but after numerous iterations I can't get it to pull everything over. Here is my current syntax.
=IF(BM5<>"External","",IF(AND(S5=VLOOKUP(A5,ExternalOnly,5,FALSE),A5=VLOOKUP(A5,ExternalOnly,1,FALSE)),S5,"")
Add an additional ')' at the end of the formula and see if this works.
i.e.
try this
=IF(BM5<>"External","",IF(AND(S5=VLOOKUP(A5,ExternalOnly,5,FALSE),A5=VLOOKUP(A5,ExternalOnly,1,FALSE)),S5,""))
I use this:
INDEX($E$1:$E$7,MATCH(A7,$D$1:$D$7,0))
Here is a sample table to illustrate. the formula is in the cells of column B (B7 in this case). How it works is the match finds the corresponding entry in the target list ($D$1:$D$7) for our selected value (A7). It returns the index from that list and the INDEX() function lets us select a different column from the matched row to return.
A B C D E
------ --- --- ------ ---
011597 99 012062 3
012062 3 012142 8
012136 3 011597 99
012142 8 012136 3
014157 2 014157 2
011582 87 011582 87
011707 101 011707 101

Resources