BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

5 REPLIES 5
Ksharp
Super User

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}
PGStats
Opal | Level 21

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
Ksharp
Super User

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;
CharlotteCain
Quartz | Level 8

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

Ksharp
Super User
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/

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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