JMP Script, filling in blanks of a column with character value - sas-jmp

I am pulling in a text file of data and using jmp script to clean it up. One column (Merch State) is populated with state initials (such as AL, CA, NJ, NY) and can have blanks. The blanks are due to international transactions and should be filled with International. I am having a tough time creating the correct script to replace any blanks within the column to International.
Any advice or assistance with this would be greatly appreciated.

You don't indicate which version of JMP you're using so I'll guess JMP 13.
One option is to select the column and use Cols > Recode... and for the blank entry, just type in International and you can have it updated in-place or into a new column.
You can also select "Script" and it'll add a data table script variable which does the Recode. Here's what it made for me which you may be able to use directly.
Current Data Table() << Begin Data Update;
For Each Row(
:Merch State = Match( :Merch State, "", "International", :Merch State )
);
Current Data Table() << End Data Update;

Related

Call Incremental Datasets Created by Macro Function

I have a macro variable called max_attempts I created from a a PROC SQL that equals 4 for my current datafile. Then, I used a macro function to create datasets up to max_attempts so now I have attempt1_table, attempt2_table, attempt3_table, and attempt4_table. Now I'm having trouble merging the 4 datasets.
data final_table;
set attempt1_table - attempt&max_attempts._table;
run;
The inputted datafile will have a different max_n each time, so I'm using macros to account for that.
The - shortcut only works if the number is at the end of the dataset name. Rename your datasets to be round_table1, round_table2, etc.:
data final_table;
set round_table1 - round_table&max_n.;
run;
Use the trimmed option of the into :macrovar clause in order to remove the leading spaces that cause set attempt1_table - attempt&max_attempts._table; to resolve into erroneous syntax.
Example:
proc sql noprint;
select <computation-for-max-attempts>
into :max_attempts trimmed /* removes leading spaces when column is numeric */
from ...
;
quit;
Thank you everyone for your help! It was two issues, the number has to be at the end of the dataset name when using the - shortcut and using trimmed to remove leading spaces.
proc sql feedback;
select max(max_attempts)
into: max_attempts trimmed
from analysis_data;
quit;
data analysis_table;
set unknown_table attempt_table1 - attempt_table&max_attempts;
run;

How can I use 'update where' select in FoxPro?

I am totally new to FoxPro (and quite fluent with MySQL).
I am trying to execute this query in FoxPro:
update expertcorr_memoinv.dbf set 'Memo' = (select 'Memo' from expertcorr_memoinv.dbf WHERE Keymemo='10045223') WHERE Keydoc like "UBOA"
I got the error:
function name is missing )
How can I fix it?
In FoxPro SQL statements you would not 'single-quote' column names. In Visual FoxPro version 9 the following sequence would run without errors:
CREATE TABLE expertcorr_memoinv (keydoc Char(20), keymemo M, Memo M)
Update expertcorr_memoinv.dbf set Memo = (select Memo from expertcorr_memoinv.dbf WHERE Keymemo='10045223') WHERE Keydoc like "UBOA"
If you would provide a few sample data and an expected result, we could see whether the line you posted would do what you want after correcting the single-quoted 'Memo' names.
NB 1: "Memo" is a reserved word in FoxPro.
NB 2: As you know, the ";" semicolon is a line-continuation in Visual FoxPro, so that a longer SQL statement can be full; of; those;
So that the Update one-liner could be written as:
Update expertcorr_memoinv ;
Set Memo = (Select Memo From expertcorr_memoinv ;
WHERE Keymemo='10045223') ;
WHERE Keydoc Like "UBOA"
NB 3: Alternatively, you can SQL Update .... From... in Visual FoxPro, similar to the Microsoft SQL Server feature. See How do I UPDATE from a SELECT in SQL Server?
I would do that just as Stefan showed.
In VFP, you also have a chance to use non-SQL statements which make it easier to express yourself. From your code it feels like KeyMemo is a unique field:
* Get the Memo value into an array
* where KeyMemo = '10045223'
* or use that as a variable also
local lcKey
lcKey = '10045223'
Select Memo From expertcorr_memoinv ;
WHERE Keymemo=m.lcKey ;
into array laMemo
* Update with that value
Update expertcorr_memoinv ;
Set Memo = laMemo[1] ;
WHERE Keydoc Like "UBOA"
This is only for divide & conquer strategy that one may find easier to follow. Other than that writing it with a single SQL is just fine.
PS: In VFP you don't use backticks at all.
Single quotes, double quotes and opening closing square brackets are not used as identifiers but all those three are used for string literals.
'This is a string literal'
"This is a string literal"
[This is a string literal]
"My name is John O'hara"
'We need 3.5" disk'
[Put 3.5" disk into John's computer]
There are subtle differences between them, which I think is an advanced topic and that you may never need to know.
Also [] is used for array indexer.
Any one of them could also be used for things like table name, alias name, file name ... (name expression) - still they are string literals, parentheses make it a name expression. ie:
select * from ('MyTable') ...
copy to ("c:\my folder\my file.txt") type delimited

How can I insert a string in an IO stream in Ruby?

I'm trying to write a Ruby script that will tweak a SQL dump (taken from pg_dump) so it can set up a table cleanly.
So far it's been all good the way I've set it up; I've been able to File.read the file, insert a word, append some stuff to the end, and File.write the file again.
However, I'm now working with a dump that's nearly 7 GB, and it won't cope (File.read is raising EINVAL errors, and there's no trouble with the filename). So I want to use a single stream to find the right spot to insert that word, and then jump to the end and append the extra stuff.
But I can't insert that word. I want to change
DROP TABLE public.programmes;
SET search_path = public, pg_catalog;
to
DROP TABLE public.programmes CASCADE;
SET search_path = public, pg_catalog;
but using file_stream.puts (#write and #<< aren't any better), I end up overwriting part of the following line:
DROP TABLE public.programmes CASCADE;
ch_path = public, pg_catalog;
... and I'd rather not have to loop (read eight characters, seek back eight characters, write previous eight characters) all the way to the end of the file, 7 GB away.
(I might be okay with doing it back to the start of the file – that's only 460 B – but I'd still have to know how to insert some characters at the start.)
Is there a way to do this?
Since the place where CASCADE needed to go was so close to the start, I ended up writing eight characters to the file first, then appending the results of pg_restore. Then I could loop through the file stream from the start and drop the string into place...
# Could be any eight characters, but these are a valid SQL comment in case it fails
File.write(path, "-------\n")
system("pg_restore #{pgr_options} >> #{path}")
File.open(path, 'r+') do |stream|
content = ''
stream.pos = 8
# The semicolon is needed to delimit the table name
content << stream.getc until content =~ /(DROP TABLE public.[a-z_]*);/
stream.rewind
stream << content[0..-2] << ' CASCADE;'
... before jumping to the end and appending stuff.
stream.seek 0, :END
stream.puts "ALTER TABLE ONLY blah blah blah..."
end

I want fetch substring from in oracle table between last '/' and before '.' from last in images table

I want to fetch substring from string in column between last '/' and last '.' .
Here is sample date for IMAGE_PATH column name:
sph/images/30_Fairhall_Court.jpeg
sph/images/9_Pennethorne_House.jpeg
rbkc/images/TAVISTOCK_CRESCENT.jpeg
haringey/images/399932thumb.jpg
urbanchoice/images/18190862.jpg
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png
housingmoves/images/No14_Asterid Heights_DS37620.jpg
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png
So the required output is like
30_Fairhall_Court
9_Pennethorne_House
TAVISTOCK_CRESCENT
399932thumb
18190862
f13c10d2-2692-457d-a208-8bb9e10b27dc
No14_Asterid Heights_DS37620
f13c10d2-2692-457d-a208-8bb9e10b27dc
Please suggest how to fetch. I need to update another blank column in table with this value. The table has around 10 lacks records.
One of possible solutions is to use functions substr() and instr() with negative third parameter:
select image_path,
substr(image_path,
instr(image_path, '/', -1) + 1,
instr(image_path, '.', -1)-instr(image_path, '/', -1) - 1) img
from test
SQL Fiddle
Results:
IMAGE_PATH IMG
-------------------------------------------------------- -------------------------------------
sph/images/30_Fairhall_Court.jpeg 30_Fairhall_Court
sph/images/9_Pennethorne_House.jpeg 9_Pennethorne_House
rbkc/images/TAVISTOCK_CRESCENT.jpeg TAVISTOCK_CRESCENT
haringey/images/399932thumb.jpg 399932thumb
urbanchoice/images/18190862.jpg 18190862
wandle/images/f13c10d2-2692-457d-a208-8bb9e10b27dc.png f13c10d2-2692-457d-a208-8bb9e10b27dc
housingmoves/images/No14_Asterid Heights_DS37620.jpg No14_Asterid Heights_DS37620
wandle/ima.ges/f13c10d2-2692-457d-a208-8bb9e10b27dc.png f13c10d2-2692-457d-a208-8bb9e10b27dc
This regex works with the sample data you provided:
select regexp_substr(image_path
, '(/)([a-z0-9_ \-]+)(\.)([a-z]+)$'
, 1
, 1
, 'i'
, 2)
from t23
/
We have to include all the optional parameters after pattern so we can use the subexpr parameter to select just the filename element. Find out more.
As far as the updating goes, a million row table isn't that big. Given that you have to update all the rows there's not much you can do to tune it. Just issue the UPDATE statement and let it rip.
"its not working"
Hmmm, here's a SQL Fiddle which proves it does work. You've probably introduced a typo.
"The regexp looks unnecessary complex. Why not simply"
Perhaps it is too complicated. However your simplified version doesn't produce the correct result if there's more than one dot in the IMAGE_PATH. If that's never going to happen then your solution works just fine.

carriage returns or new Line with linq

so i have a problem with carriage return I have web page that have a text area and for a report I need to count how many people wrote in this text area that are save in a sql table call sell and column note
so with linq I do
var count_notes = Vmsb.Sell.Where(v => !(v.Note == null || v.Note.Trim() == string.Empty)).Count();
Vmsb is my dbcontext
the problem is when my note just have carriage returns or "\r\n", it count it and that is not what I want
the sql that generate is something like this
SELECT count(note) FROM [dbo].sell AS v WHERE (( NOT (v.[nota] IS NULL OR N'' = (LTRIM(RTRIM(v.[nota])))))
so how can i do to not take in count the "\r\n"
I never was aware of the fact that L/RTRIM in SQL does not remove newline characters. The only way I can think of is
v.Note.Trim().Replace("\r", "").Replace("\n", "")
(sorry Brice, did not see your comment when wrote this)
Of course it is much easier to prevent input like these "empty" strings to enter the database. I would consider a clean up action and adding a gatekeeper to the application.

Resources