Hi, I am a long time STATA user who is required to use SAS for a certain project.
How can I search for a list of 20 diagnosis codes over 25 diagnosis variables (diag1-diag25) and have it flag a new variable as one?
This would be similar to the STATA code:
local diagcode
"xxxxx"
"xxxxx"
"xxxxx"
foreach x of varlist diag1-diag25 {
replace diabetes = 1 if `x' == diagcode
Thank you!
Please always post sample data in form of a data step and then show us the desired result. This helps us to better understand what you have and also test our code against your sample.
List-processing is "better" implemented in other languages. Below code does what you're after if I've understood your Stata code correctly.
data have;
array diag {25} $10 (25*'other code');
output;
diag4='code3';
diag15='code2';
output;
run;
proc format;
invalue diagcode
'code1'=1
'code2'=1
'code3'=1
;
run;
data want;
flag=0;
set have;
array diags {*} diag:;
do _i=1 to dim(diags);
if input(diags[_i],diagcode.)=1 then
do;
flag=1;
leave;
end;
end;
run;
What does "it flag a new variable as one" actually mean?
WHICHN or WHICHC (depending on whether your diagnosis variables and diagnosis codes are numeric or character) may work.
A brief example of what I think you are doing. Diag holds the diagnosis codes, the array c has the code values you are looking for and the array f has variable that hold flag values of 1 or 0 for found.
data work.example; informat diag1 - diag3 $2.; input diag1 - diag3 ; array d diag: ; /* this holds the values of the codes to search for*/ array c (4) $ 2 _temporary_ ('Ab','Ac','Ba','Bb'); /* and results size should match array c*/ array f Ab Ac Ba Bb; do i=1 to dim(c); f[i]=whichc(c[i],of d(*))>0; end; datalines; Ab Ac Ad Ac Ac Ae Ba Bb Bc Bb Bb Bb ; run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.