sas calculation in table [closed] - sas

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a dataset like this:
data a;
input a b $ c;
cards;
3 + 5
4 + 4
5 - 2
6 * 5
7 / 2
;run;
I want add a new column D that calculates its value from columns A, B, and C:
A B C D
=== === === ===
3 + 5 8
4 + 4 8
5 * 2 10
6 - 5 1
7 / 2 3.5

How about this.
data a;
input a b $ c;
select(b);
when('+') d=a+c;
when('-') d=a-c;
when('*') d=a*c;
when('/') d=a/c;
otherwise;
end;
cards;
3 + 5
4 + 4
5 - 2
6 * 5
7 / 2
;;;;
run;

Related

Any other way to write a loop for multiplication table to take run time values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
To print multiplication tables, I'm using a for loop which has fixed values up to 10th times table only. I need multiplication table as run time values. Is there an alternate loop to take the values at run time? Here is the code which i tried:
scanf(&num);
for(i=num;i<=num;i++)
for (j=1;j<=10;j++)
This is not a Python For loop
In python,you can use the Range function to have a nice multiplication table.
def table_choice(my_choice=None):
for a in range(10):
if my_choice != None:
print('{0} x {1} = {2}'.format(my_choice, a, my_choice * a))
else:
for b in range(10):
print('{0} x {1} = {2}'.format(a, b, a * b))
table_choice(my_choice = 7)
OUTPUT:
7 x 0 = 0
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
In case you execute table_choice() you will get the full table
See the Range documentation in : https://docs.python.org/3/library/functions.html#func-range

Sorting sequence based on greatest common division [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a question. I write this comparator:
bool cmp(int a, int b)
{
return __gcd(a, b) > 1;
}
and, for example:
if I have these numbers:
2 5 6 7 8 12 15 19 20
my code outputs:
20 15 12 8 6 2 5 7 19
it's okay..
but for example:
1 2 3 4 5 6 7 8 9
my code outputs
1 2 3 4 5 6 7 8 9
How can I do this?
this sequence should be something like this:
9 6 3 (...)
Your comparator does not establish strict weak ordering, so results are undefined.
To make proper comparator you must make sure that following is true:
cmp(a, a) == false — your comparator does not pass the test on cmp(2, 2)
cmp(a, b) == true → cmp(b, a) == false — your comparator does not pass the test on cmp(2, 4)
cmp(a, b) == true and cmp(b, c) == true → cmp(a, c) == true — your comparator does not pass the test on cmp(2, 6) and cmp(6, 3)

select n number of element from an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There is given array of size > n , we need to select n number of element from an array.
For example : array contain 112 element and n = 50, so select 50 number such that distance between every two selected number is more or less equal (equal distance is not possible of course except for size%n == 0 ).
If anyone suggest any idea that would work .
Example :
array = 1 2 3 4 5
n = 1
output : 1 or any another number depending on proposed algo.
n = 2
output : 1 3 or 2 4 or 1 4...
n = 3
output : 1 3 5
n = 4
output : 1 3 4 5 or 1 2 4 5
n = 5 :
output 1 2 3 4 5
Basically in case of n=1,2,4 there are more then one possible combination so I need to devise an algo which would pick numbers in uequally distributed manner.
One approach would be dividing the number of elements by the number of desired elements in the selection in floating point, and using rounding to determine the index:
double dist = ((double)size) / n;
int *res = new int[n];
for (int i = 0 ; i != n ; i++) {
res[i] = orig[round(dist*i)];
}
For your example of 112 and 50 the value of dist would be 2.24 and the sequence of indexes selected from the array would be
0 0
1 2
2 4
3 7
4 9
5 11
......
45 101
46 103
47 105
48 108
49 110

Return the lines in which the numbers are [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have next file with thousands of lines
1 2 3 4
1 4 5
6
5 6 8 9
5
2 4 7 9
5 8 10
1 3 5 9
.......
Each line (the line number represents the regions) has maximum 4 values (represent labels) and these values are between 0 and 1000.
And I want to know in which lines (regions) are the values (labels) and order them, for that I will need a loop but I do not how.
For instance for the label number 1 is found in line (region) 1,2 and 8 so the output must be 1: 1 2 8
I want this output:
1: 1 2 8
2: 1 6
3: 1 8
4: 1 2 6
5: 2 4 5 7 8
6: 3 4
7: 6
8: 4 7
9: 4 6 8
10: 7
....
The output is the sorted labels from 0 to 1000 and the regions in which they are found.
I was thinking in a loop in Linux or c++ any idea?
You can have a std::map<int, std::list<int>> that maps int to list<int>.
For each value use:
auto iter = map.find(row);
if (iter != m.end() )
{
(iter->second).push_back(val);
} else {
map[row] = std::list<int>(1, val);
}
Then use an iterator and print all the key,value pairs at the end.
Perl to the rescue:
perl -lne 'push #{ $h{$_} }, $. for split
}{
print "$_: #{ $h{$_} }" for sort { $a <=> $b } keys %h
' < input.txt
$. is the line number, you just add it to the hash table for each of the numbers on the line. At the end, you sort the keys of the hash table numerically and print the corresponding arrays.

how do you implement allocating strategy with c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two workers.If I have configured a strategy that 60% tasks are allocate to A worker and the rest to B worker.
how do you implement with c++.
what is your suggestion?
map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));
map<string,int> m_count;
m_count.insert(make_pair("total",0));
map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{
m_count.insert(make_pair("countA",1));
}
else
{
int &c = it->second;
if(itc!=m_count.end()&&itp!=m_percent.end())
{
float f=(c+1)*100/(itc->second+1)*100.0
if (f<=itp->second)
{
it->second=it->second+1;
}
}
}
if(itc!=m_count.end())
{
itc->second=itc->second+1;
}
If you're talking about number of tasks with no regard to complexity, simply keep a count of how many jobs have been allocated to each. Let's call these counts countA for the jobs allocated to A, and count for the total number of jobs (for simplicity in calculations), and initialise them to zero.
Then, when a job comes in, allocate as per the following:
if count is equal to zero, allocate it to A and increment both countA and count.
otherwise, if countA / count is less than 0.6, allocate it to A and increment both countA and count.
otherwise allocate it to B and just increment count.
This will, over the long term, tend to average out allocations so that A gets 60%:
countA count countA/count allocateTo
------ ----- ------------ ----------
0 0 ? A
1 1 1.000 B
1 2 0.500 A
2 3 0.667 B
2 4 0.500 A
3 5 0.600 B
3 6 0.500 A
4 7 0.571 A
5 8 0.625 B
5 9 0.556 A
6 10 0.600 B
6 11 0.545 A
7 12 0.583 A
8 13 0.615 B
8 14 0.571 A
9 15 0.600 B
9 16 0.563 A
10 17 0.588 A
11 18 0.611 B
11 19 0.579 A
12 20 0.600
... and so on.