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

Hello,

 

I have the follow repeat coding.  Any idea how to simply them?  Thanks.

data want (drop = A B C);
	set test;
	if A in ('No', 'n') then AA=0;
	if A in ('Yes', 'y') then AA=1;

	if B in ('No', 'n') then BB=0;
	if B in ('Yes', 'y') then BB=1;

	if C in ('No', 'n') then CC=0;
	if C in ('Yes', 'y') then CC=1;

run;
1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

Arrays are good for this:

data want (drop = A B C);
 set test;
 array answers (*) A B C;
 array answers_flag (*) AA BB CC;
 do i = 1 to dim(answers); 
  if answers(i) in ('No', 'n') then answers_flag(i)=0;
  else if answers(i) in ('Yes', 'y') then answers_flag(i)=1;
end;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data want;
 set test;
 array t a b c;
 array u  aa bb cc;
 do over t;
  if t in ('No', 'n') then u=0;
  else if t in ('Yes', 'y') then u=1;
 end;
 drop a b c;
run;

 Alternatively, you can use PROC Format aka User defined format/In-format as a look up. 

SASKiwi
PROC Star

Arrays are good for this:

data want (drop = A B C);
 set test;
 array answers (*) A B C;
 array answers_flag (*) AA BB CC;
 do i = 1 to dim(answers); 
  if answers(i) in ('No', 'n') then answers_flag(i)=0;
  else if answers(i) in ('Yes', 'y') then answers_flag(i)=1;
end;
run;
ybz12003
Rhodochrosite | Level 12

Thank you so much for all of your wonderful help!

ballardw
Super User

For some of these things I prefer informats.

proc format library=work;
invalue myyesno (upcase)
'NO','N' = 0
'YES','Y' =1
' '=.
other= _error_;
run;

data example;
   infile datalines truncover;
   informat a myyesno.;
   input a;
datalines;
Y
y
Yes
YES
yEs
N
n
NO
no
nO

YN
3
;



Note the definition has (UPCASE). That means SAS will make the value uppercase before comparing to the key values. So if you have issues with humans entering data where the case may change as shown then you still get expected results. The explicit case of missing, ' ', allows the use of the other predicate to create warnings for invalid/unexpected data values as shown.

 

The informats can be used with an input statement if you already have the data read:

 

AA = input(a,myyesno.);

BB = input(b,myyesno.);

CC = input(c,myyesno.);

(or array). If there were 25 of these variables the array approach becomes very preferred.

 

Note that since the informat creates a numeric value there is no $ at the start of the informat name.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 621 views
  • 4 likes
  • 4 in conversation