BookmarkSubscribeRSS Feed
wmedina
Fluorite | Level 6

Hello,

I am having trouble creating a new variable using conditions, I've tried data steps but to no avail.

 

My data set looks like this right now:

 

 

A B C D E
1 . 1 1 .
. 1 . . . 
1 . 1 . 1 

 

I need to look like this 

 

A B C D E R
. .  .  . 1
. 1 . . . .
. . . . . 1

So the idea that i've used is if the sum of a -- d is greater than 1 then set R equal to 1 else .  and then drop the observations if 1 is present in a & b & c & d & e but its not doing it for me perhaps its due to missing values. 

 

code i've used so far:

 

data campZ;
	set campY;
	select;
	when (sum(Macroscopic -- Symbolic > 1)) Random = 1;
    otherwise;
end; 
run;

I've tried Proc SQL as well but I have been mainly focusing on the data step but any help will be great. 

 

Thank you!

Will 

 

 

4 REPLIES 4
mohamed_zaki
Barite | Level 11
data need (Drop= i numMissing);
set have;
array varx(*) A--E; 
numMissing = cmiss(A,B,C,D);
	if numMissing < 3 then DO; 
		R = 1;
			Do i =1 to dim(varx);
				varx(i)=.;
			End;
	End;
	Else R=.;
run;
mohamed_zaki
Barite | Level 11

Another way

 

data need (Drop= i);
set have;
array varx(5) A--E; 
	if SUM(A,B,C,D) > 1 then DO; 
		R = 1;
			Do i =1 to dim(varx);
			varx(i)=.;
			End;
	End;
	Else R=.;
run;
Ksharp
Super User
data have;
input A B C D E;
cards;
1 . 1 1 .
. 1 . . . 
1 . 1 . 1
;
run;
data want;
 set have;
 if sum(of A--E) > 1 then do;
  Random = 1;
  call missing(of A--E);
 end;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 795 views
  • 1 like
  • 3 in conversation