BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

10 REPLIES 10
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

art297
Opal | Level 21

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

 

Tom
Super User Tom
Super User

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;
ballardw
Super User

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

 

 

ybz12003
Rhodochrosite | Level 12

I passed both base and adv tests.

Oligolas
Barite | Level 11

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 -

Tom
Super User Tom
Super User

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;



ybz12003
Rhodochrosite | Level 12

Thanks for your effort, Oligolas.

ybz12003
Rhodochrosite | Level 12

Thanks for all of your great help.  I learn a lot here.

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!

What is Bayesian Analysis?

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.

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
  • 10 replies
  • 2246 views
  • 6 likes
  • 7 in conversation