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;
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;
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.
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;
Thank you so much for all of your wonderful help!
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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: