BookmarkSubscribeRSS Feed
stellapersis7
Obsidian | Level 7

Hi all,

I have trouble understanding macro.

I have the following code to run for a dataset x, I want a macro code for this to run in x,y, and z data sets.

Can anyone please help me with this?

Here is the code:

 

data ci;
set CI16;
array d(25) ICD_DX_CD_1 - ICD_DX_CD_25;
DMCAT=0;
do I = 1 to 25;
if d(i) = SUBSTR(D{I},1,3)= 'E08' OR SUBSTR(D{I},1,3) = 'E09' OR SUBSTR(D{I},1,3)= 'E10' OR SUBSTR(D{I},1,3)= 'E11' OR SUBSTR(D{I},1,3)= 'E13'

then DMCAT=1;
end;
run;

Thanks a lot

6 REPLIES 6
PaigeMiller
Diamond | Level 26

If I am understanding you properly

 

%macro dothis(dataset=);
data ci;
set &dataset;
array d(25) ICD_DX_CD_1 - ICD_DX_CD_25;
DMCAT=0;
do I = 1 to 25;
if d(i) = SUBSTR(D{I},1,3)= 'E08' OR SUBSTR(D{I},1,3) = 'E09' OR SUBSTR(D{I},1,3)= 'E10' OR SUBSTR(D{I},1,3)= 'E11' OR SUBSTR(D{I},1,3)= 'E13'

then DMCAT=1;
end;
run;
%mend;

%dothis(dataset=x)
%dothis(dataset=y)
%dothis(dataset=z)
--
Paige Miller
sbxkoenk
SAS Super FREQ
%MACRO HelpWithMacros(inds=,outds=);
 data &outds.;
  set &inds.;
/* put the body of the datastep here */
 run;
%MEND HelpWithMacros;

options mprint symbolgen mlogic;
%HelpWithMacros(inds=work.X,outds=work.X_out);
%HelpWithMacros(inds=work.Y,outds=work.Y_out);
%HelpWithMacros(inds=work.Z,outds=work.Z_out);
/* end of program */

BR, Koen

stellapersis7
Obsidian | Level 7

Hi sbxkoenk

my new code for 3 datasets was :

 

 

%MACRO HW2_DX(inds=,outds=);

data &outds.;

 set &inds.;

 

  array d(26) PRMRY_DX_CD ICD_DX_CD_1 - ICD_DX_CD_25;

DMCAT=0;

%do I = 1 %to 26;

if d(i) = SUBSTR(D{I},1,3)= 'E08' OR SUBSTR(D{I},1,3) = 'E09' OR SUBSTR(D{I},1,3)= 'E10' OR SUBSTR(D{I},1,3)= 'E11' OR SUBSTR(D{I},1,3)= 'E13'

 then DMCAT=1;

%end;

%MEND HW2_DX;

 

options mprint symbolgen mlogic;

%HW2_DX(inds=hw2j.syhdr_commercial_inpatient_2016,outds=hw2_1);

%HW2_DX(inds=hw2j.syhdr_commercial_outpatient_2016,outds=hw2_2);

%HW2_DX(inds=hw2j.syhdr_medicaid_inpatient_2016,outds=hw2_3);

This does not work and gives an error of "variable I is uninitialized"

need some help!

sbxkoenk
SAS Super FREQ

Hello,

 

There is NO variable in your dataset with the name "i".
There is only a macro variable with the name "i".
If you want to resolve the macro variable (to get its value), you need to specify &i. .

(the dot after the macro variable name is only needed if "confusion" is possible)

 

You don't need quotes here (as you deal with a numeric value)

, but in case you need quotes (for a string value) , just remember this :
Macro variables in SAS won't resolve when they are in single quotes, like '&myvar' . They need to be in double quotes, "&myvar" , in order to resolve properly.

 

BR, Koen

PaigeMiller
Diamond | Level 26

%DO works on macro variables, such as &i. DO works on data step variables, such as i.

 

In your code, use DO instead of %DO (and TO instead of %TO)

--
Paige Miller
Astounding
PROC Star

To encapsulate this program in a macro, suitable for 3 data sets, you need to overcome a couple of problems.

 

First, the output data set name is ci.  That's fine when you have one data set to process.  But if you process three data sets, each run replaces ci with a different output.  You will need 3 data set names, not 1.

 

Second, the code itself could use a little bit if cleaning up.  In particular:

  • SUBSTR is inefficient
  • Continuing to search all 25 variables is inefficient once you already have a single match that sets DMCAT to 1.

Try it this way:

 

%macro set_dmcat (dsn);
   data ci_&dsn;
      set &dsn;
      array icd_dx_cd_ {25};  /* autoatically uses the array name plus a numeric suffix */
      dmcat=0;
      do i=1 to 25  until (dmcat=1);
         if icd_dx_cd_{i} in: ('E08', 'E09', 'E10', 'E11', 'E13') then dmcat=1;
      end;
run;
%mend set_dmcat;

%set_dmcat (x)
%set_dmcat (y)
%set_dmcat (z)

Using =: compares whether two strings are equal, but makes the comparison using the number of characters in the shorter string.  That lets  you eliminate SUBSTR when you want to examine the beginning of a string.

Adding the UNTIL condition lets you stop examining the rest of the diagnosis codes once a match has been found.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 1340 views
  • 0 likes
  • 4 in conversation