what is the differences between +n and (n) in bit operations? - abap

I've found two parameters defined like these:
&TM_PERIOD+4&/&TM_PERIOD(4)&
It's to pass data from a database to a form.
If the format of the data would be DDMMYYYY what are differences between those two parameters?

if TM_PRIOD is in form of DDMMYYYY then
TM_PERIOD(4) equals DDMM
TM_PERIOD+4 equals YYYY
the (4) means 4 characters
the +4 means after the 4th character
TM_PERIOD+1(2) = DM
(2 characters after the first)

These are not bit operations. +n specifies a string offset and (n) specifies the length.
They can be used independently of each other as well, so you can use just +n or just (n).
So:
data: lv_text(20) type c.
lv_text = "Hello".
write: / lv_text+2(3).
would output 'llo', for example.

Related

Is there a SQL function statement which returns an array within limits and replaces digits with * to reduce the size

Need to write a sql function that takes two different integers with the same length (same amount of digits) as arguments: one lower threshold and one upper threshold.
And the function should return a vector with all whole numbers between the two thresholds - but to reduce the array length, your function is supposed to return a wildcard character instead of the digits where possible.
Example: output of the function for a lower threshold L = 3778 and an upper threshold U = 9423
To further clarify, the line in the example showing 941* has one digit replaced by the wild card character and hence represents all values from 9410 - 9419
The line in the example showing 93* has two digits replaced by the wildcard character and represents all values from 9300 - 9399
And so on.
9423
9422
9421
9420
941*
93*
.
378*
3779
3778

How to split a series of number and compare it with another set of digit?

Say I have a no. 20101105, I need to compare it with a series of other nos. say 20110105 , 20090105 and find the nearest no. of it.
I don't want to compare it on the whole, I need to compare it each digit wise by parsing thru it and then see which is the closest.
Can someone suggest on how to do this in ABAP language?
In general You should mention some more information. For example, are the numbers really integers ? Then You can put them into an internal table and sorting all of them is the easiest solution to find any "nearest" number relating to an actual scanned. This is just like integers work in sort, they are sorted like numbers, my friend. But If You want it character-wise ( what really makes no sense, if the numbers are integers ) i give You some help with this character-comparison in a do-loop, taking smaller string-length as iterator-counter. I omitted the else, that's Your "homework". :-D
DATA:
lv_length1 TYPE i,
lv_length2 TYPE i,
lv_cnt TYPE i,
lv_teststr1 TYPE string VALUE '123456',
lv_teststr2 TYPE string VALUE '1235'.
lv_length1 = strlen( lv_teststr1 ).
lv_length2 = strlen( lv_teststr2 ).
IF lv_length1 GE lv_length2.
DO lv_length2 TIMES.
IF lv_teststr2+lv_cnt(1) NE lv_teststr1+lv_cnt(1).
BREAK-POINT.
ENDIF.
ADD 1 TO lv_cnt.
ENDDO.
ENDIF.
The counter variable is also the index of, in this case, the first not matching character. This gets the job done.
Coded and tested by me just right now.
I don't know if I understood but maybe this helps.
report znearest.
data lv_value(8) type n.
parameters p_value(8) type n. " ---------> The value
select-options s_values for lv_value. " -> The list
start-of-selection.
data: wa like line of s_values,
lv_dif(8) type n,
lv_nearest(8) type n,
lv_nearest_dif(8) type n,
lv_first type c.
loop at s_values into wa.
lv_dif = abs( p_value - wa-low ). " Calculate the difference
if lv_first is initial.
lv_nearest_dif = lv_dif.
lv_first = 'X'.
endif.
if lv_dif le lv_nearest_dif. " Compare the differences
lv_nearest = wa-low.
lv_nearest_dif = lv_dif.
endif.
endloop.
write: 'The nearest from', p_value, 'is', lv_nearest.
Hope it helps.

SAS specific observation format

I would like to create a new variable in SAS which takes the value 1 if an observation in the variable "TEXT" contains 8 numbers. The problem is, that TEXT is a character variable. Is it possible to make some kind of a format search in SAS?
I assume by '8 numbers' you actually mean 8 digits. For 8 separate numbers, that would be different.
So something like the code below might help.
The modifier 'kd' meaning KEEP DIGITS in COMPRESS function does the magic here:
data indata;
length TEXT $20;
input TEXT;
datalines;
a
123
12345678
A12345678
;
run;
data outdata;
set indata;
length TEXT_DIGITS $20 _8_DIGIT_INDICATOR 3;
TEXT_DIGITS = compress(TEXT, , 'kd');
if length(TEXT_DIGITS)=8 then _8_DIGIT_INDICATOR = 1;
run;
Adjust the logic as you need - e.g. if no other character in input value is allowed or something else.
Also functions like ANYDIGIT, NOTDIGIT might be useful.

How to write number with sign on the left and thousands separator point

I am holding the number in character format in abap. Because I have to take the minus from right to left. So I have to put the number to character and shift or using function 'CLOI_PUT_SIGN_IN_FRONT' I'm moving minus character to left.
But after assigning number to character it doesn't hold the points. I mean my number is;
1.432- (as integer)
-1432 (as character)
I want;
-1.432 (as character)
is there a shortcut for this or should I append some string operations.
Edit:
Here is what I'm doing now.
data: mustbak_t(10) TYPE c,
mustbak like zsomething-menge.
select single menge from zsomething into mustbak where something eq something.
mustbak_t = mustbak.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
CHANGING
VALUE = mustbak_t.
write: mustbak_t.
If you're on a recent release, you could use string templates - you'll have to add some black magic to use a country that confoirms to your decimal settings, though:
DATA: l_country TYPE t005x-land,
l_text TYPE c LENGTH 15,
l_num TYPE p LENGTH 6.
SELECT SINGLE land
INTO l_country
FROM t005x
WHERE xdezp = space.
l_num = '-123456'.
l_text = |{ l_num COUNTRY = l_country }|.
WRITE: / l_text.
In this case, you need a country code to pass to the COUNTRY parameter as described in the format options. The values of the individual fields, namely T005X-XDEZP are described in detail in the country-specific formats.
tl;dr = Find any country where they use "." as a thousands separator and "," as a decimal separator and use that country settings to format the number.
You could also use classic formatting templates, but they are hard to handle unless you have a fixed-length output value:
DATA: l_text TYPE c LENGTH 15,
l_num TYPE p LENGTH 6 DECIMALS 2.
l_num = '-1234.56'.
WRITE l_num TO l_text USING EDIT MASK 'RRV________.__'.
CONDENSE l_text NO-GAPS.
WRITE: / l_text.
Here's another way, which i finally got working:
DATA: characters(18) TYPE c,
ints TYPE i VALUE -222333444.
WRITE ints TO characters. "This is it... nothing more to say.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
CHANGING
value = characters.
WRITE characters.
Since integers are automatically printed with the thousands separator, you can simply output them to a char data object directly using WRITE TO with no aditions..... lol
DATA: currency TYPE cdcurr,
characters(18) TYPE c,
ints TYPE i VALUE -200000.
currency = ints.
WRITE currency TO characters CURRENCY 'USD' DECIMALS 0.
CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
CHANGING
value = characters.
.
WRITE: / 'example',characters.
This prints your integer as specified. Must be apparently converted to a currency during the process.

Convert Numeric to Character 20

Hi I am building a dataset, but the data I am merging is in different formats.
From the Excel sheet i import its in numeric 8, and the other 2 datasets im merging to are character 20, so I want to change the numeric 8 to char 20.
How can I change the variable acctnum, to char 20? (I also want to keep this as its name, as I presume a new variable will be created)
data WORK.T82APR;
set WORK.T82APR;
rename F1 = acctnum f2 = tariff;
run;
proc contents data=T82APR;
run;
While this thread is already dead, I thought I'd way in and answer why the 14 digits conversion became in E notation.
Typically, or rather, unless otherwise specified, numeric formats in SAS use BEST12 format. As such, when a numeric value is longer than 12 characters (including any commas and periods), BEST12 chooses E notation as the best way to format the value.
The input function, in that case receives the formatted value put(acctnum, BEST12.). There would've been 2 ways around it.
Either use
input(put(acctnum, 14.), $20.);
Or, change the format of the variable using the format statement (directly in a data step or with proc datasets like) - this has the added benefit that if you open the table in SAS, you will see the 14 digits and not the scientific formatted value.
proc datasets library=work nolist;
modify dsname;
format acctnum 14.;
run;
Vincent
Try this:
data WORK.T82APR ;
set WORK.T82APR;
acctnum = put(F1, $20.);
rename f2 = tariff;
run;
Ok, I didn't pay attention to your own rename statement, so I adjusted my answer to reflect that now.