Good morning,
I am trying to create a macro to rename variables based on different conditions. I got N1=18 and N2=16 from Proc means statement. Then, I would like to assign 'num' to different values based on different 'If-then' conditions. Finally, I call the macro. However, I got an error massage from Log, showing below.
proc contents data=test out=have (keep=NAME);
run;
data temp;
set have;
if substr(name,1, 6)='nad_dx' then N1=1;
else if substr(name,1, 8)='nad_cert' then N2=1;
run;
Proc means data=temp n;
Var _numeric_;
Output out=want sum =;
Run;
data macro;
set want;
if substr(name,1, 6)='nad_dx' then call symputx('num', N1, 'G');
if substr(name,1, 8)='nad_cert' then call symputx ('num', N2, 'G');
run;
data rename;
set test;
rename nad_dx_1-nad_dx_&num=bd_dx_1-bd_dx_#
rename nad_cert_1-nad_cert_&num=bd_dx_cert_1-bd_dx_cert_#
run;
27 data rename;
28 set test;
29 rename nad_dx_1-nad_dx_&num=bd_dx_1-bd_dx_#
-
73
76
WARNING: Apparent symbolic reference NUM not resolved.
WARNING: Apparent symbolic reference NUM not resolved.
30 rename nad_cert_1-nad_cert_&num=bd_dx_cert_1-bd_dx_cert_#
-
3
76
WARNING: Apparent symbolic reference NUM not resolved.
WARNING: Apparent symbolic reference NUM not resolved.
ERROR 73-322: Expecting an =.
ERROR 76-322: Syntax error, statement will be ignored.
31 run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.RENAME may be incomplete. When this step was stopped there were 0
observations and 34 variables.
And I would like to the final statement showing as below, please advice how to fix it. Thanks.
rename nad_dx_1-nad_dx_18=bd_dx_1-bd_dx_18;
rename nad_cert_1-nad_cert_16=bd_dx_cert_1-bd_dx_cert_16;
The reason that the macro varaible is NOT created is because the CALL SYMPUTX() never runs.
But your logic seems flawed. What are you trying to do?
If you want to find how many variables in TEMP start with the characters NAD_DX then you could do something like this.
data _null_;
set test ;
array n1 nad_dx_:;
array n2 nad_cert_: ;
call symputx('n1',dim(n1));
call symputx('n2',dim(n2));
stop;
run;
data rename ;
set test;
rename nad_dx_1-nad_dx_&n1=bd_dx_1-bd_dx_&n1;
rename nad_cert_1-nad_cert_&n2=bd_dx_cert_1-bd_dx_cert_&n2;
run;
I bet that your dataset want does not have a matching value for
if substr(name,1, 6)='nad_dx'
and
if substr(name,1, 8)='nad_cert'
If it looks like the values are there, watch out for leading blanks.
Some test data in the form of a datastep would help.
Are lists of variables not "--". Anyways, if there are multple calls of symput, i.e. more rows than one resolve, then that messes up the system. You could of course simplfy the code to:
data _null_; set sashelp.vcolumn (where=(libname="WORK" and memname="TEST")) end=last; if _n_=1 then call execute('proc datasets library=work; modify test; rename '); call execute(catx('=',name,tranwrd(tranwrd(name,"nad_dx","bd_dx"),"nad_cert",bd_dx_cert"))); if last then call execute(';quit;run;'); run;
This will generate one proc datasets code which will do the renaming (note proc datasets is better at resources for these header changes). Althought the question arises, why renaming these, they must have been created somewhere, just update there, or alternatively, normalise your data - this makes it far easier to work for programming (so data goes down as question + value pairs), you could simplfy your process then, and if at the end you need data across then transpose at the end.
Your proc means won't create a variable called NAME unless there is a variable called name in your dataset. However, if there were, it would contain a numeric value because that is what you instructed proc means to produce.
Art, CEO, AnalystFinder.com
The reason that the macro varaible is NOT created is because the CALL SYMPUTX() never runs.
But your logic seems flawed. What are you trying to do?
If you want to find how many variables in TEMP start with the characters NAD_DX then you could do something like this.
data _null_;
set test ;
array n1 nad_dx_:;
array n2 nad_cert_: ;
call symputx('n1',dim(n1));
call symputx('n2',dim(n2));
stop;
run;
data rename ;
set test;
rename nad_dx_1-nad_dx_&n1=bd_dx_1-bd_dx_&n1;
rename nad_cert_1-nad_cert_&n2=bd_dx_cert_1-bd_dx_cert_&n2;
run;
Have you taken the free online SAS Programming 1 course? This page has a link to the course: https://support.sas.com/edu/elearning.html?ctry=us&productType=library
I passed both base and adv tests.
Hi,
the variable num is not created in 'macro' if the condition is not fulfilled but you still try to resolve it in 'rename'
Proc means data=temp n;
Var _numeric_;
Output out=want sum =;
Run;
%macro doit;
%let num=;
data macro;
set want;
if substr(name,1, 6)='nad_dx' then call symputx('num', N1, 'G');
if substr(name,1, 8)='nad_cert' then call symputx ('num', N2, 'G');
run;
data rename;
set test;
%if &num. ne %then %do;
rename nad_dx_1-nad_dx_&num=bd_dx_1-bd_dx_#
rename nad_cert_1-nad_cert_&num=bd_dx_cert_1-bd_dx_cert_#
%end;
run;
%mend doit;
%doit;
- Cheers -
Actually defining a %LOCAL macro variable and then using CALL SYMPUTX() with the 'G' or 'GLOBAL' option will cause confusion.
The CALL SYMPUTX() will update the GLOBAL macro variable named NUM but the later references to &NUM inside the macro will use the value of the LOCAL macro variable with the same name.
@Oligolas wrote:
Hi,
the variable num is not created in 'macro' if the condition is not fulfilled but you still try to resolve it in 'rename'
Proc means data=temp n; Var _numeric_; Output out=want sum =; Run; %macro doit; %let num=; data macro; set want; if substr(name,1, 6)='nad_dx' then call symputx('num', N1, 'G'); if substr(name,1, 8)='nad_cert' then call symputx ('num', N2, 'G'); run; data rename; set test; %if &num. ne %then %do; rename nad_dx_1-nad_dx_&num=bd_dx_1-bd_dx_# rename nad_cert_1-nad_cert_&num=bd_dx_cert_1-bd_dx_cert_# %end; run; %mend doit; %doit;
Thanks for your effort, Oligolas.
Thanks for all of your great help. I learn a lot here.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.