Help using Base SAS procedures

Call Symputx doesn't work?

Accepted Solution Solved
Reply
Super Contributor
Posts: 319
Accepted Solution

Call Symputx doesn't work?

[ Edited ]

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;

 


Accepted Solutions
Solution
‎06-14-2017 08:46 PM
Super User
Super User
Posts: 7,039

Re: Call Symputx doesn't work?

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


All Replies
Super User
Posts: 7,768

Re: Call Symputx doesn't work?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,942

Re: Call Symputx doesn't work?

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.

PROC Star
Posts: 7,468

Re: Call Symputx doesn't work?

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

 

Solution
‎06-14-2017 08:46 PM
Super User
Super User
Posts: 7,039

Re: Call Symputx doesn't work?

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;
Super User
Posts: 11,343

Re: Call Symputx doesn't work?

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

 

 

Super Contributor
Posts: 319

Re: Call Symputx doesn't work?

I passed both base and adv tests.

Frequent Contributor
Posts: 129

Re: Call Symputx doesn't work?

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 -

Super User
Super User
Posts: 7,039

Re: Call Symputx doesn't work?

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;



Super Contributor
Posts: 319

Re: Call Symputx doesn't work?

Thanks for your effort, Oligolas.

Super Contributor
Posts: 319

Re: Call Symputx doesn't work?

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 254 views
  • 6 likes
  • 7 in conversation