BookmarkSubscribeRSS Feed
atex
Calcite | Level 5

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!

2 REPLIES 2
Patrick
Opal | Level 21

@atex

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;

 

ballardw
Super User

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2092 views
  • 0 likes
  • 3 in conversation