DATA Step, Macro, Functions and more

any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 137
Accepted Solution

any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

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

 


Accepted Solutions
Solution
‎10-20-2016 11:46 AM
Respected Advisor
Posts: 4,654

Re: any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

[ Edited ]

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

View solution in original post


All Replies
Super User
Posts: 9,682

Re: any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

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
Respected Advisor
Posts: 4,654

Re: any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

[ Edited ]

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
Super User
Posts: 9,682

Re: any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

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;
Frequent Contributor
Posts: 137

Re: any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

[ Edited ]

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 Smiley Very Happy

 

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

Super User
Posts: 9,682

Re: any programmatic way to compute sets/probabilities the problem perhaps with a SAS function?

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/
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 369 views
  • 0 likes
  • 3 in conversation