Set A | |
---|---|

A | B |

5 | 6 |

5 | 6 |

5 | 6 |

6 | 6 |

5 | 5 |

4 | 6 |

6 | 5 |

5 | 6 |

6 | 6 |

6 | 6 |

5 | 6 |

6 | 4 |

5 | 6 |

turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- any programmatic way to compute sets/probabilities...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

10-16-2016 06:11 AM

Hello all, any programmatic way to compute the problem perhaps with a SAS function?

Let x be the set of possible outcomes from rolling a dice twice, x = {(a, b) : a, b ∈ {1, 2, 3, 4, 5, 6}}.

Let A be the subset of x given by {(a, b) ∈ x : a + b ≥ 10} and let B be the subset {(a, b) ∈ x : a = 5}.

(a) Write the sets A and B in a new variable

(b) Write A and B in list form (i.e. as a list of elements enclosed in {. . .}).

(c) Write A ∪ B, A ∩ B and A \ B in list form in new variable

(d) write the set x \ A in in a new var.

Accepted Solutions

Solution

10-20-2016
11:46 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to CharlotteCain

10-17-2016 12:16 AM - edited 10-17-2016 12:19 AM

I am not sure what you mean by "variable", but here is how to create macro variables using the set operators in SQL:

```
data x;
do a = 1 to 6;
do b = 1 to 6;
output;
end;
end;
run;
proc sql noprint;
create table a as select * from x where a+b>=10;
create table b as select * from x where a=5;
select cats("(",a,",",b,")") into :a separated by "," from a;
select cats("(",a,",",b,")") into :b separated by "," from b;
select cats("(",a,",",b,")") into :aub separated by ","
from ((select * from a) union (select * from b));
select cats("(",a,",",b,")") into :anb separated by ","
from ((select * from a) intersect (select * from b));
select cats("(",a,",",b,")") into :asb separated by ","
from ((select * from a) except (select * from b));
select cats("(",a,",",b,")") into :xsa separated by ","
from ((select * from x) except (select * from a));
quit;
%put a={&a};
%put b={&b};
%put aUb={&aub};
%put anb={&anb};
%put asb={&asb};
%put xsa={&xsa};
```

PG

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to CharlotteCain

10-16-2016 11:21 PM

Charlotte,

It is more like data simulation question.You'd better post it as IML forum.

```
proc iml;
n=200; /*the sample size*/
call randseed(12345678);
dice=sample((1:6),n);
x=shape(dice,0,2);
a=x[loc(x[,+]>=10),];
b=x[loc(x[,1]=5),];
/*(a) Write the sets A and B in a new variable*/
print a[l='Set A' c={a b}],b[l='Set B' c={a b}];
/*(b) Write A and B in list form (i.e. as a list of elements enclosed in {. . .}).*/
temp='{'+rowcat(char(a))+'}';
print temp[l='Set A'];
temp='{'+rowcat(char(b))+'}';
print temp[l='Set B'];
/* (c) Write A ∪ B, A ∩ B and A \ B in list form in new variable*/
temp=a//b;
AUB='{'+rowcat(char(temp))+'}';
print AUB[l='A ∪ B'];
temp_a='{'+rowcat(char(a))+'}';
temp_b='{'+rowcat(char(b))+'}';
ANB=t(xsect(temp_a,temp_b));
print ANB[l='A ∩ B'];
A_B=t(setdif(temp_a,temp_b));
print A_B[l='A \ B '];
quit;
```

OUTPUT:

Set A | |
---|---|

A | B |

5 | 6 |

5 | 6 |

5 | 6 |

6 | 6 |

5 | 5 |

4 | 6 |

6 | 5 |

5 | 6 |

6 | 6 |

6 | 6 |

5 | 6 |

6 | 4 |

5 | 6 |

Set B | |
---|---|

A | B |

5 | 4 |

5 | 3 |

5 | 1 |

5 | 6 |

5 | 6 |

5 | 6 |

5 | 4 |

5 | 5 |

5 | 2 |

5 | 4 |

5 | 3 |

5 | 4 |

5 | 3 |

5 | 4 |

5 | 6 |

5 | 2 |

5 | 3 |

5 | 6 |

5 | 4 |

5 | 6 |

Set A |
---|

{ 5 6} |

{ 5 6} |

{ 5 6} |

{ 6 6} |

{ 5 5} |

{ 4 6} |

{ 6 5} |

{ 5 6} |

{ 6 6} |

{ 6 6} |

{ 5 6} |

{ 6 4} |

{ 5 6} |

Set B |
---|

{ 5 4} |

{ 5 3} |

{ 5 1} |

{ 5 6} |

{ 5 6} |

{ 5 6} |

{ 5 4} |

{ 5 5} |

{ 5 2} |

{ 5 4} |

{ 5 3} |

{ 5 4} |

{ 5 3} |

{ 5 4} |

{ 5 6} |

{ 5 2} |

{ 5 3} |

{ 5 6} |

{ 5 4} |

{ 5 6} |

A ∪ B |
---|

{ 5 6} |

{ 5 6} |

{ 5 6} |

{ 6 6} |

{ 5 5} |

{ 4 6} |

{ 6 5} |

{ 5 6} |

{ 6 6} |

{ 6 6} |

{ 5 6} |

{ 6 4} |

{ 5 6} |

{ 5 4} |

{ 5 3} |

{ 5 1} |

{ 5 6} |

{ 5 6} |

{ 5 6} |

{ 5 4} |

{ 5 5} |

{ 5 2} |

{ 5 4} |

{ 5 3} |

{ 5 4} |

{ 5 3} |

{ 5 4} |

{ 5 6} |

{ 5 2} |

{ 5 3} |

{ 5 6} |

{ 5 4} |

{ 5 6} |

A ∩ B |
---|

{ 5 5} |

{ 5 6} |

A \ B |
---|

{ 4 6} |

{ 6 4} |

{ 6 5} |

{ 6 6} |

Solution

10-20-2016
11:46 AM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to CharlotteCain

10-17-2016 12:16 AM - edited 10-17-2016 12:19 AM

I am not sure what you mean by "variable", but here is how to create macro variables using the set operators in SQL:

```
data x;
do a = 1 to 6;
do b = 1 to 6;
output;
end;
end;
run;
proc sql noprint;
create table a as select * from x where a+b>=10;
create table b as select * from x where a=5;
select cats("(",a,",",b,")") into :a separated by "," from a;
select cats("(",a,",",b,")") into :b separated by "," from b;
select cats("(",a,",",b,")") into :aub separated by ","
from ((select * from a) union (select * from b));
select cats("(",a,",",b,")") into :anb separated by ","
from ((select * from a) intersect (select * from b));
select cats("(",a,",",b,")") into :asb separated by ","
from ((select * from a) except (select * from b));
select cats("(",a,",",b,")") into :xsa separated by ","
from ((select * from x) except (select * from a));
quit;
%put a={&a};
%put b={&b};
%put aUb={&aub};
%put anb={&anb};
%put asb={&asb};
%put xsa={&xsa};
```

PG

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to CharlotteCain

10-17-2016 06:19 AM

Charlotte,

You can use the following to replace PG's data step part, if you would like to use SQL.

```
data x;
n=200;
call streaminit(12345678);
do i=1 to n;
a=ceil(6*rand('uniform'));
b=ceil(6*rand('uniform'));
output;
end;
keep a b;
run;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Ksharp

10-17-2016 11:43 AM - edited 10-17-2016 11:49 AM

Good evening Genius @Ksharp and @PGStats Thank you so much for the help. Well, I honestly didn't realise this could have posted in IML forum. Both, Amazingly elegant approach and neat. PG Sir, I wanted in a new variable just for reading ease and your understaing was right.

Xia, Sorry for the late response as I am aware by the time I get home from work and attending afternoon college, you must be sleeping in China. I will study your code over night, understand and will follow up with you if i don't understand some parts. And I trust I can bother PG while you are asleep if he stays warm enough in freezing Canada

As always I can't thank you enough,

Regards from England,

Charlotte

Btw, Can you share me a link to the detailed documentation of IML

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

Posted in reply to CharlotteCain

10-18-2016 12:29 AM

Charlotte,
I think PG's code would be good for you. It is easy to use and understand .
If you want learn something from IML. Rick start a very good IML blog, you can learn a lot from it.
For completely information about IML, Check its documentation, you can find it easily at support.sas.com
http://blogs.sas.com/content/iml/