user2395238
user2395238

Reputation: 900

How to print two sets of variable series next to each other in SAS?

I have a SAS dataset where I keep 50 diagnoses codes and 50 diagnoses descriptions. It looks something like this:

data diags;
     set diag_list;
     keep claim_id diagcode1-diagcode50 diagdesc1-diagdesc50;
run;

I need to print all of the variables but I need diagnosis description right next to corresponding diagnosis code. Something like this:

proc print data=diags;
    var claim_id diagcode1 diagdesc1 diagcode2 diagdesc2 diagcode3 diagdesc3; *(and so on all the way to 50);
run;

Is there a way to do this (possibly using arrays) without having to type it all up?

Upvotes: 1

Views: 222

Answers (2)

Richard
Richard

Reputation: 27508

Here is some example SAS code that uses actual ICD 10 CM codes and their descriptions and @Reeza proc print:

%* Copy government provided Medicare code data zip file to local computer;

filename cms_cm url 'https://www.cms.gov/Medicare/Coding/ICD10/Downloads/2020-ICD-10-CM-Codes.zip' recfm=s;
filename zip_cm "%sysfunc(pathname(work))/2020-ICD-10-CM-Codes.zip" lrecl=200000000 recfm=n ;

%let rc = %sysfunc(fcopy(cms_cm, zip_cm));
%put %sysfunc(sysmsg());

%* Define fileref to the zip file member that contains ICD 10 CM codes and descriptions;

filename cm_codes zip "%sysfunc(pathname(zip_cm))" member="2020 Code Descriptions/icd10cm_codes_2020.txt";

%* input the codes and descriptions, there are 72,184 of them;
%* I cheated and looked at the data (more than once) in order 
%* to determine the variable sizes needed;

data icd10cm_2020;
  infile cm_codes lrecl=250 truncover;
  attrib
    code length=$7
    desc length=$230
  ;
  input
    code 1-7 desc 9-230;
  ;
run;

* simulate claims sample data with mostly upto 8 diagnoses, and
* at least one claim with 50 diagnoses;

data have;
  call streaminit(123);
  do claim_id = 1 to 10;
    array codes(50) $7   code1-code50;
    array descs(50) $230 desc1-desc50;
    call missing(of code:, of desc:);

    if mod(claim_id, 10) = 0
      then top = 50;
      else top = rand('uniform', 8);

    do _n_ = 1 to top;
      p = ceil(rand('uniform', n));     %* pick a random diagnosis code, 1 of 72,184;
      set icd10cm_2020 nobs=n point=p;  %* read the data for that random code;
      codes(_n_) = code;
      descs(_n_) = desc;
    end;
    output;
  end;
  stop;
  drop top;
run;

%macro loop_names(n=);
    %do i=1 %to &n;
        code&i  desc&i. 
    %end;
%mend;

ods _all_ close;
ods html;


proc print data=have;
    var claim_id %loop_names(n=50);
run;

Upvotes: 0

Reeza
Reeza

Reputation: 21274

Here's one approach then, using Macros. If you have other variables make sure to include them BEFORE the %loop_names(n=50) portion in the VAR statement.

*generate fake data to test/run solution;
data demo;
    array diag(50);
    array diagdesc(50);

    do claim_id=1 to 100;

        do i=1 to 50;
            diag(i)=rand('normal');
            diagdesc(i)=rand('uniform');
        end;
        output;
    end;
run;


%macro loop_names(n=);
    %do i=1 %to &n;
        diag&i  diagdesc&i. 
    %end;
%mend;


proc print data=demo;
    var claim_ID %loop_names(n=20);
run;

Upvotes: 2

Related Questions