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;

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
  • 2 replies
  • 1461 views
  • 0 likes
  • 3 in conversation