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

The data looks like this

data temp;

input var1 var2 var3;

datalines;

1 1 0

0 0 0

0 1 0

1 1 1

;

There are a few observations with all 0s, how can I efficiently create a new dichotomous variable (e.g., var4) which assigns 1 to those observations and 0 otherwise? The output may look like:

1 1 0 0

0 0 0 1

0 1 0 0

1 1 1 0

This should work but it looks unattractive:

data want; set temp;

var4 = 0;

if var1 = 0 and var2 = 0 and var3 = 0 then var4 = 1;

run;

Btw, will this work?

data want; set temp;

var4 = 0;

array abc var1 var2 var3;

do i = 1 to 3;

if abc(i) = 0 then  var4 = 1;

end; run;

1 ACCEPTED SOLUTION

Accepted Solutions
SASFREAK
Obsidian | Level 7

data want;

  set temp;

  if sum(of var1 - var3) eq 0 then var4=1;

  else var4=0;

run;

how about this ?

View solution in original post

7 REPLIES 7
SASFREAK
Obsidian | Level 7

data want;

  set temp;

  if sum(of var1 - var3) eq 0 then var4=1;

  else var4=0;

run;

how about this ?

NonSleeper
Quartz | Level 8

This is a working solution for this one. But what will it be on a more general issue? I mean, for example, let's say that for variables var1 to var3, value "1" is now "NYC" and "0" is now "Buffalo"

JerryLeBreton
Pyrite | Level 9

Maybe this:

       var4 = ^(var1=var2=var3);

Sorry - missed the point.  You want something like:

     var4 = (var1=var2=var3 ='Buffalo');

Your array processing would be fine too of course, and more generic.

 

NonSleeper
Quartz | Level 8

I was a bit suspicious over my array, because it seems to me the array will assigns 1 if any, but not ALL, of these variables (var1 var2 var3) is 0.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Then you would need to count the words or replace:

array var{3};

if countw(catx(',',of var{*}),"NYC") > 0 then var4=1;  /* i.e. if NYC exists in the string then the result is 1 */

else var4=0;

JerryLeBreton
Pyrite | Level 9

Try:  

var4 = ^sum(of var1-var3);

The ^ produces a Boolean result since any non-zero value is considered true.

MadhuKorni
Quartz | Level 8

data Want;

set Have;

count = 0;

array ary(*) var1-var3;

do i = 1 to dim(ary);

if ary(i) = 0 then count+1;

end;

if count = 3 then var4=1;else var4= 0;

drop count i;

run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 1994 views
  • 0 likes
  • 5 in conversation