DATA Step, Macro, Functions and more

Better way to create indicator variable in PROC SQL

Reply
Frequent Contributor
Posts: 77

Better way to create indicator variable in PROC SQL

I wonder if there is a better way to create a indicator variable as shown in the following sql statement:

proc sql;
create table ds3 as
select a.*,
case
when b.id = '' then 0
else 1
end as ind
from ds1 a left join ds2 b
on a.id = b.id
; quit;
Super User
Posts: 10,018

Re: Better way to create indicator variable in PROC SQL

I think your code is good enough.If you want more brief.
[pre]
data class;
set sashelp.class end=last; output;
if last then do;
name='Peter';sex=' '; output;
end;

proc sql;
create table temp as
select a.*,not missing(a.sex) as ind
from class as a left join class as b on a.sex=b.sex;
quit;
[/pre]


Ksharp
SAS Employee
Posts: 104

Re: Better way to create indicator variable in PROC SQL

You can make your code a touch more efficient with a little boolean logic:

proc sql;
create table ds3 as
select a.*
, b.id NE '' as ind
from ds1 a left join ds2 b
on a.id = b.id
;
quit;

Message was edited by: SASJedi Message was edited by: SASJedi
Ask a Question
Discussion stats
  • 2 replies
  • 4223 views
  • 2 likes
  • 3 in conversation