BookmarkSubscribeRSS Feed
jlc35
Calcite | Level 5

I'm trying to do something similar to the following. I have data that is similar to the following (apologies, I'm not sure how best to format this in this forum:

SVI                                     Prop_Vax
Low Vulnerability                 .2300
High Vulnerability                .5000
Moderate Vulnerability        .4230

I am trying to create a macro, and as part of that macro, I want to have a data step that creates two new variables a "link" variable for a drill down graph, and a categorical SVI variable utilizing numbers rather than the full text value. Right now, I have the code as such:

%LET SVI1=Low Vulnerability;
%LET SVI2=Moderate Vulnerability;
%LET SVI3=High Vulnerability;

%macro COVID_Data;
data covid2;
set covid;
SVIcat2=.;
Link=" ";
%do i=1 %to 3;
%if SVI="&&SVI&i" %then SVIcat2=i;
%else %if SVI=" " %then SVIcat2=.;
%end;
%do i=1 %to 3;
%if SVI="&&SVI&i" %then link="vuln&i..html";
%else %if SVI=" " %then link=" ";
%end;
run;
%mend COVID_Data;

However, when I run the macro, it doesn't actually produce values in the columns for the "SVIcat2" and "Link" variables, and I can't figure out why. Can someone explain what I am doing incorrectly?

2 REPLIES 2
WarrenKuhfeld
Rhodochrosite | Level 12

Use the DATA step and arrays, not macros for this. Understand that the macro processing executes first, before the data are read.

Kurt_Bremser
Super User

A condition like this

%if SVI="&&SVI&i"

can NEVER be true, as the text SVI can never be equal to a text containing quotes, no matter what the macro variables resolve to.

The mistake comes from the misunderstanding of what a macro does, and when it does it. The macro can never have access to data that will be present once the code executes after the macro created it. You cannot use values in variables that will be present only once the data step created by the macro is compiled and executed.

 

Macro is for handling code, not for handling data. Print that on a large sheet in very large letters and fix it above your desk.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 407 views
  • 0 likes
  • 3 in conversation