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

These are 02 data sets below:

 

South.dat

S 43 3 27

S 44 3 24

S 45 3  2

 

North.dat

N 21 5 41 1

N 87 4 33 3

N 65 2 67 1

N 66 2  7  1

 

DATA southentrance;

   INFILE '/folders/myfolders/sas littlebook/South.dat';

  INPUT Entrance $ PassNumber PartySize Age;

PROC PRINT DATA = southentrance;

   TITLE 'South Entrance Data';

RUN;

 

DATA northentrance;

 INFILE '/folders/myfolders/sas littlebook/North.dat';

   INPUT Entrance $ PassNumber PartySize Age Lot;

   PROC PRINT DATA = northentrance;

   TITLE 'North Entrance Data';

RUN;

 

/* Create a data set, both, combining northentrance and southentrance*/

/* Create a variable, AmountPaid, based on value of variable Age*/

 

DATA both;

   SET southentrance northentrance;

   IF Age = . THEN AmountPaid = .;

      ELSE IF Age < 3  THEN AmountPaid = 0;

      ELSE IF Age < 65 THEN AmountPaid = 35;

      ELSE AmountPaid = 27;

RUN;

PROC PRINT DATA = both;

   TITLE 'Both Entrances';

RUN;

 

How do I write this program with Proc sql to combine these 02 sets together:

 

thanks

 

TK

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try the proc sql with union 

 

 

data have1;
input Entrance $ PassNumber PartySize Age;
cards;
S 43 3 27
S 44 3 24
S 45 3  2
;

data have2;
input Entrance $ PassNumber PartySize Age;
cards;
N 21 5 41 1
N 87 4 33 3
N 65 2 67 1
N 66 2  7  1
;


proc sql;
create table want as select *, case when age < 3 then 0 when age < 65 then 35 when age  eq . then . else 27 end as 
amountpaid from (select * from have1 
union 
select * from have2);
quit;
Thanks,
Jag

View solution in original post

4 REPLIES 4
SuryaKiran
Meteorite | Level 14

Your are joining the tables vertically, so you can use set operators in PROC SQL (OUTER UNION, UNION, EXCEPT, INTERSECT). For your requirement OUTER UNION with corresponding keyword with CASE expression can give you the result.

 

PROC SQL;
Create table want as 
select *,CASE WHEN AGE=. THEN  .
			  WHEN Age < 3  THEN 0
			  WHEN Age < 65 THEN 35
			  ELSE 27 END AS AmountPaid
	FROM southentrance
OUTER UNION CORR
select *,CASE WHEN AGE=. THEN  .
			  WHEN Age < 3  THEN 0
			  WHEN Age < 65 THEN 35
			  ELSE 27 END AS AmountPaid
	FROM northentrance
;
QUIT;
Thanks,
Suryakiran
Jagadishkatam
Amethyst | Level 16

Please try the proc sql with union 

 

 

data have1;
input Entrance $ PassNumber PartySize Age;
cards;
S 43 3 27
S 44 3 24
S 45 3  2
;

data have2;
input Entrance $ PassNumber PartySize Age;
cards;
N 21 5 41 1
N 87 4 33 3
N 65 2 67 1
N 66 2  7  1
;


proc sql;
create table want as select *, case when age < 3 then 0 when age < 65 then 35 when age  eq . then . else 27 end as 
amountpaid from (select * from have1 
union 
select * from have2);
quit;
Thanks,
Jag
Reeza
Super User

Make sure to use UNION ALL otherwise duplicate records are excluded. 

bondtk
Quartz | Level 8

thanks Jag 

 

it worked, I tried it with union all....

 

thanks

 

TK

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 860 views
  • 2 likes
  • 4 in conversation