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
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;
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;
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;
Make sure to use UNION ALL otherwise duplicate records are excluded.
thanks Jag
it worked, I tried it with union all....
thanks
TK
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.