SAS: assigned character value gets reduced - sas

In a data step process, I create a new variable with character value. However, character values in the output data got reduced from "CL_FA_IF" to "CL". What is the problem?
else if var in ("CL", "FA", "IF") then var2 = "CL_FA_IF"

Related

How to parse a string with comma separated values in AWS IoT SQL?

I am trying to parse a long string with comma-separated values such as "lat,long,distance,,elevation". String is actually quite long and I need to fetch each value and save the fetched values in different columns in dynamodb. I am using dyamodbv2 rule. Functions I found that could be useful were substring(String, Int [, Int]), length(String), indexof(String, String) and get().
For example I get data like this:
{
LOCATION_DATA: "lat,long,distance,,elevation"
}
Here is what I have done so far,
//first value - 0 to next comma
substring(LOCATION_DATA, 0, indexof(LOCATION_DATA, ',')) as latitude,
//second value - substring starting from last substring to next comma
substring(substring(LOCATION_DATA, indexof(LOCATION_DATA, ',') +1 ) ,
0,
indexof(substring(LOCATION_DATA, indexof(LOCATION_DATA, ',') +1 ), ',')
) as longitude,
...
But this gets too verbose and moving to next comma-separated value increasingly difficult. Is there a way to convert comma-separated values to array and then fetch them with get(0), get(1).. ? I have to fetch around 20 fields this way!
Also, the values can be of varying length, and some fields can be empty, such as value between "distance,,elevation" in example strings. These empty values can be ignored.
As far as I now, there is no way I can store and create custom functions, or use any other function than provided in http://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html.
In rails, you can convert a string to array based on a separator
Example
LOCATION_DATA = "lat,long,distance,,elevation"
myarray = LOCATION_DATA.split(',')
Then you can use
myarray[0]="lat"
myarray[1]="long"
myarray[2]="distance"
myarray[3]=""
myarray[4]="elevation"
You can also convert these strings to integer or float as:
myarray[0].to_i
myarray[2].to_f
Hope This Helps

Remove everything but numbers from a cell

I have an excel sheet where i use the follwoing command to get numbers from a cell that contains a form text:
=MID(D2;SEARCH("number";D2)+6;13)
It searches for the string "number" and gets the next 13 characters that comes after it. But some times the results get more than the number due to the fact these texts within the cells do not have a pattern, like the example below:
62999999990
21999999990
11999999990
6299999993) (
17999999999)
21914714753)
58741236714 P
18888888820
How do i avoid taking anything but numbers OR how do i remove everything but numbers from what i get?
You can user this User Defined Function (UDF) that will get only the numbers inside a specific cell.
Code:
Function only_numbers(strSearch As String) As String
Dim i As Integer, tempVal As String
For i = 1 To Len(strSearch)
If IsNumeric(Mid(strSearch, i, 1)) Then
tempVal = tempVal + Mid(strSearch, i, 1)
End If
Next
only_numbers = tempVal
End Function
To use it, you must:
Press ALT + F11
Insert new Module
Paste code inside Module window
Now you can use the formula =only_numbers(A1) at your spreadsheet, by changing A1 to your data location.
Example Images:
Inserting code at module window:
Executing the function
Ps.: if you want to delimit the number of digits to 13, you can change the last line of code from:
only_numbers = tempVal
to
only_numbers = Left(tempVal, 13)
Alternatively you can take a look a this topic to understand how to achieve this using formulas.
If you are going to go to a User Defined Function (aka UDF) then perform all of the actions; don't rely on the preliminary worksheet formula to pass a stripped number and possible suffix text to the UDF.
In a standard code module as,
Function udfJustNumber(str As String, _
Optional delim As String = "number", _
Optional startat As Long = 1, _
Optional digits As Long = 13, _
Optional bCaseSensitive As Boolean = False, _
Optional bNumericReturn As Boolean = True)
Dim c As Long
udfJustNumber = vbNullString
str = Trim(Mid(str, InStr(startat, str, delim, IIf(bCaseSensitive, vbBinaryCompare, vbTextCompare)) + Len(delim), digits))
For c = 1 To Len(str)
Select Case Asc(Mid(str, c, 1))
Case 32
'do nothing- skip over
Case 48 To 57
If bNumericReturn Then
udfJustNumber = Val(udfJustNumber & Mid(str, c, 1))
Else
udfJustNumber = udfJustNumber & Mid(str, c, 1)
End If
Case Else
Exit For
End Select
Next c
End Function
I've used your narrative to add several optional parameters. You can change these if your circumstances change. Most notable is whether to return a true number or text-that-looks-like-a-number with the bNumericReturn option. Note that the returned values are right-aligned as true numbers should be in the following supplied image.
By supplying FALSE to the sixth parameter, the returned content is text-that-looks-like-a-number and is now left-aligned in the worksheet cell.
If you don't want VBA and would like to use Excel Formulas only, try this one:
=SUMPRODUCT(MID(0&MID(D2,SEARCH("number",D2)+6,13),LARGE(INDEX(ISNUMBER(--MID(MID(D2,SEARCH("number",D2)+6,13),ROW($1:$13),1))* ROW($1:$13),0),ROW($1:$13))+1,1)*10^ROW($1:$13)/10)

Excel VBA Macro: Using regex to return adjacent row data

Alright; so here's the whole thing I'm suppose to do.
Input a number that corresponds with a number in Data Worksheet Column A and return the adjacent row data.
I want it to return the adjacent cells; example. If it finds 052035 in cell A5378, I Want it to return the data or cell numbers B5378, C5378
EDIT: I've deleted my code; since it didn't really follow with a good way to do it.
Worksheet Structure for Data:
A 1-7800ish[6 Digit number 1-9]
B 1-7800ish Area Codes
C 1-7800ish City/States
The data by the way; is a relatively large set that I got from a query on a SQL-Server. The string number that I'm looking for should have no duplicates based on my original query. [I grouped by before copying it over]
If ya'll have resources for a quick introduction to VB from a programming perspective that'll be helpful. I can program in C/C++ but the syntax in VB is a little weird to me.
If your end goal is to simply find the exact match in column A, and return the values in corresponding row, columns B & C, Regular Expressions is the wrong tool for the job. Use built in functions like Match.
I still don't understand the point of this exercise, as, the data is already arranged in columns A, B and C., you could simply use AutoFilter... This subroutine simply tells you that the value is found (and returns the corresponding data) or not found.
I have tested this (made a small change in dimensioning vals variable)
Sub Foo()
Dim valToLookFor As String
Dim rngToLookAt As Range
Dim foundRow As Long
Dim vals() As Variant
valToLookFor = "052035"
Set rngToLookAt = Range("A:A")
If Not IsError(Application.Match(valToLookFor, rngToLookAt, False)) Then
foundRow = Application.Match(valToLookFor, rngToLookAt, False)
ReDim vals(1)
vals(0) = rngToLookAt.Cells(foundRow).Offset(0, 1).Value
vals(1) = rngToLookAt.Cells(foundRow).Offset(0, 2).Value
'Alternatively, to return the cell address:
'vals(0) = rngToLookAt.Cells(foundRow).Offset(0,1).Address
'vals(1) = rngToLookAt.Cells(foundRow).Offset(0,2).Address
MsgBox Join(vals, ",")
Else:
Erase vals
MsgBox valToLookFor & " not found!", vbInformation
End If
End Sub
Here is proof that it works:

converting character to numeric (SAS)

I am trying to convert a character column to numeric and I have tried using:
var=input(var,Best12.);
var=var*1;
Both of them returned character columns, and there is only 1 warning message:
"Character values have been converted to numeric values at the places given by: (Line):(Column). 7132:4".
Is there another what to do this conversion inside SAS?
(my apologies if this is trivial)
Thanks!
What you're doing will work if you assign the result to a new variable:
data tmp;
char='1';
run;
data tmp;
set tmp;
num=char*1;
run;
proc contents; run;

Add Semicolon to each value (each line) in a cell

I have the following values in a single cell let be A1
1234
567
454
Likewise all the A(N) are filled with values. N various from 1000 to 1500
I want this to get converted as
1234;567;454
Any shortcut available?
Edit: Sorry, had not read your questions properly...
You could write a vba-script like that:
Sub test()
Dim result As String
result = Replace(ActiveCell.value, Chr(10), ";")
ActiveCell.Offset(1, 0).Select
ActiveCell.value = result
End Sub
It will take the active cell, replace all newlines by semicolons and put the result in the next line.
Edit: Another version doing this for multiple cells:
Sub test()
Dim value As String
Do
value = ActiveCell.value
If (value = "") Then Exit Do
ActiveCell.Offset(0, 1).value = Replace(ActiveCell.value, Chr(10), ";")
ActiveCell.Offset(1, 0).Select
Loop While (True)
End Sub
This version will start at the active cell, and loop through all cell below until it finds an empty cell.
The replaced value is written into the cell next to the original one. If you want to replace the original value, remove .Offset(0, 1).
The second parameter is the value to be replaced, it's Chr(10), the Newline character in our case.