BookmarkSubscribeRSS Feed
lueryy2000
Calcite | Level 5
Hi,

I am pretty new to macro and need some help here. Below is my dataset:

data mydata;
input var1 var1_suffix var2 var2_suffix var3 var3_suffix var4 var4_suffix var5 var5_suffix;
datalines;
4.2 1 1.8 0 2.3 0 3.5 0 2.3 1
3 0 2.5 0 5.2 1 6.4 0 2.4 0
;
run;

var1 through var5 are five variables of interest. var1_suffix through var5_suffix are corresponding indicators which take values of 0 or 1. If its value=1, then I want set the corresponding variable value to missing. For example, var1=4.2 and var1_suffix=1 so I will change the value of var1 to missing, the result should be var1=. and var1_suffix=1. I wrote a macro to do this:
%MACRO mymacro(varLIST);
DATA mydataout;
SET mydata;
%LET K=1;
%LET a=%SCAN(&varLIST,&K);
%DO %WHILE("a" NE "");
%IF &a_suffix=1 %THEN &a=.;
%LET K=%EVAL(&K+1);
%LET a=%SCAN(&varLIST,&K);
%END;
RUN;
%MEND;
%mymacro(var1 var2 var3 var4 var5);

However, SAS gave an error message "Apparent symbolic reference A_SUFFIX not resolve". I guess I shouldn't use &a_suffix to stand for the variables of var1_suffix to var5_suffix, but I don't know how to fix it. Any input will be appreciated.

Lu
8 REPLIES 8
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Suggest using the OPTIONS statement below to maximize your compilation phase output for diagnostic and desk-checking:

OPTIONS SOURCE SOURCE2 MACROGEN SYMBOLGEN MPRINT /* MLOGIC */ ;

Also, you make reference to macro variable "a" but you do so without the ampersand.

Your reference to using the underscore creates a challenge because a_suffix is a legitimate macro variable - you will need to include a period-delimiter when making such a reference, however also you will need to code &&&a._suffix to get the correct macro variable resolution in your code.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

macro variable resolution site:sas.com

macro variable not resolved site:sas.com
lueryy2000
Calcite | Level 5
Thanks. I changed &a_suffix to &a._suffix, but it still didn't work. Any suggestion?
art297
Opal | Level 21
Lu,

If you really, REALLY are trying to learn how to write macros, then let the forum know so that others will provide feedback regarding how to do what you want by writing a macro.

However, your problem is easily solved in a data step using arrays. I changed the names of your suffix variables so that they would be more ammenable to puting them in a variable list.

data mydata (drop=i);
array vars(*) var1-var5;
array suffix(*) suffix_var1-suffix_var5;
input var1 suffix_var1 var2 suffix_var2
var3 suffix_var3 var4 suffix_var4
var5 suffix_var5;
do i=1 to dim(vars);
if suffix(i) eq 1 then do;
call missing(vars(i));
end;
end;
datalines;
4.2 1 1.8 0 2.3 0 3.5 0 2.3 1
3 0 2.5 0 5.2 1 6.4 0 2.4 0
;
run;

HTH,
Art
lueryy2000
Calcite | Level 5
Thanks. I prefered using Macro to do this cause I have lots of variables and it is hard to calculate the total.
Cynthia_sas
SAS Super FREQ
Hi:
I don't understand what you mean when you say that you prefer macro because "it is hard to calculate the total" -- the total of what??? The total of var1-var5 after you set some of the values to missing??? the total of all the var1 values in the dataset?? the total of all the var2 values in the dataset???

In your new posting thread, I recommended you use an array approach. This modification of that program calculates the OBSTOT variable which is the sum of var1-var5 (after adjusting for missing). Then the PROC PRINT with the SUM statement sums the variables.

cynthia

[pre]
data mydata;
input var1 var1_suffix var2 var2_suffix var3 var3_suffix var4 var4_suffix var5 var5_suffix;
datalines;
4.2 1 1.8 0 2.3 0 3.5 0 2.3 1
3 0 2.5 0 5.2 1 6.4 0 2.4 0
;
run;

data setmiss;
set mydata;
array v var1-var5;
array vs var1_suffix var2_suffix var3_suffix var4_suffix var5_suffix ;
do i = 1 to 5;
putlog 'BEFORE IF statement ' i= v(i)= vs(i)=;
if vs(i) = 1 then v(i)=.;
putlog 'AFTER IF statement ' i= v(i)= vs(i)=;
end;
obstot = sum(of var1-var5);
run;

proc print data=setmiss;
title 'Making Change Using Arrays and using SUMS';
var obstot var1 var1_suffix var2 var2_suffix var3 var3_suffix var4 var4_suffix var5 var5_suffix;
sum obstot var1-var5;
run;
[/pre]
lueryy2000
Calcite | Level 5
Thank you very much. With no other choices I will use array statement. But I will still look if macro can do it since I think it is easier to change the string (var1 - var5 or var1_suffix - var2_suffix) in parameter in macro program rather than change it during the data step. Thanks again.

Lu
Cynthia_sas
SAS Super FREQ
Hi:
The SAS Macro facility is only generating code that goes to the compiler, You could just as easily make macro variables to hold your variable names and then use those macro variables in your program. Something like this:

[pre]
** you could generate these 2 macro variables with a program;
%let vlist = var1-var5;
%let vslist = var1_suffix var2_suffix var3_suffix var4_suffix var5_suffix;

data setmiss2;
set mydata;
array v &vlist;
array vs &vslist ;
do i = 1 to dim(v);
if vs(i) = 1 then v(i)=.;
end;
obstot = sum(of &vlist);
run;

proc print data=setmiss2;
title 'Making Change Using Arrays and using SUMS';
var obstot &vlist &vslist;
sum obstot &vlist;
run;
[/pre]

This approach would allow you to change the variables going to the ARRAY statement, but the basic program is unchanged, except for the substitution of the macro variable values at code compilation time. This does assume that the variables will be in a numbered list (such as var1-var5) so that the OF in the SUM statement will work. If you provided a type of variable list to be summed, you might have to change the SUM statement to get remove the OF syntax.

cynthia
lueryy2000
Calcite | Level 5
Thank you very much. I learned a lot.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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