DATA Step, Macro, Functions and more

programming arrays with part character variables

Reply
Contributor
Posts: 44

programming arrays with part character variables

Hello,

 

I'm using SAS 9.4 full edition via a virtual desktop. I have 71 co-morbidities and would like to find the most frequent ones.  I have shown 3 examples of below of how the variables are coded and then tried to code the array with these 3 example comorbidities. They aren't numbered 1-3 but rather have character coding that distinguish each comorbidity. Do I take the numbers out of the array?  Do I have to rename all of the variables?

 

CM_PULMCIRC
CM_RENLFAIL
CM_LIVER

 

data want; set have;
array CM_' ' [3] CM_' '1-CM_' '3;   
do i=1 to 3;
   if CM_' ' [i] > ' ' then do;
      diagnosis = CM_' '[i];
	output;
   end;
end;
keep diagnosis;
run;
proc freq order=freq; tables diagnosis; run;

 I 

PROC Star
Posts: 8,149

Re: programming arrays with part character variables

[ Edited ]

You could use:

 

data want; set have;
  array _CM[3] CM_PULMCIRC CM_RENLFAIL CM_LIVER;   
  do i=1 to 3;
     if not missing(_CM[i]) then do;
        diagnosis = _CM[i];
        output;
     end;
  end;
  keep diagnosis;
run;

proc freq order=freq; tables diagnosis; run;

Art, CEO, AnalystFinder.com

 

Note: (12/31/2017 5:55pm) Removed an underscore that was inadvertently left in the array name. However, if you are interested in the variable names (as @PGStats mentioned), then change 

diagnosis = _CM[i];

to

diagnosis = vname(_CM[i]);

as he suggested.

 

Esteemed Advisor
Posts: 5,481

Re: programming arrays with part character variables

[ Edited ]

Or rather

 

diagnosis = vname(_CM[i]);

PG
Super User
Super User
Posts: 7,933

Re: programming arrays with part character variables

[ Edited ]

Your question is a little confusing.  You talk about how the variables are coded but I think you meant how they are named.  How a variable is coded means the type, length and list or type of valid values.

 

If you have a series of variables that all start with the same characters you can use a colon ( : ) wildcard in the variable list for your array definition.  Just make sure that they are all of the same type (ie coded the same way). 

 

You can use the DIM() function to find out how many variables are in the array.

data want;
  set have;
  array _CM CM_: ;
  do i=1 to dim(_cm);
    if not missing(_cm(i)) then do;
      diagnosis = _cm(i);
      output;
    end;
  end;
keep diagnosis;
run;

 

Ask a Question
Discussion stats
  • 3 replies
  • 194 views
  • 3 likes
  • 4 in conversation