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

Hello

 

I am using SAS 9.3.

 

I have a report where the sources for the same report may differ so I need to rename some fields.  So if the variable field of inst_no includes those in the incl_list then rename using one field otherwise use another field.  Below is wrong but I've been working on this forever so hopefully someone can help.  Thanks.

 

%let inst_no=5555;
%let incl_list=3333, 4444, 5555, 6666;

%macro renm1;
%if &inst_no. in (&incl_list) %then %do I = 0 %to 20;
status&i=pxattr&i;
else %do i = 0 %to 20;
status&i=pxstat&i;
%end;
%mend;

Data have;
set mydata;
rename %renm1;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Something like the following would work - I'm not a fan of macro logic, in case you were wondering. 

 

data _null_;

if &inst_no in (&incl_list) then prefix='pxattr';
else prefix='pxstat';

call symputx('prefix', prefix, 'g');

run;

data have;
set mydata;
rename status1-status20 = &prefix.1-&prefix.20;
run;

View solution in original post

14 REPLIES 14
novinosrin
Tourmaline | Level 20

You need the mindelimiter option in your macro statement. I hope I have corrected your code

 

%let inst_no=5555;
%let incl_list=3333, 4444, 5555, 6666;

%macro renm1/mindelimiter=',';
%if &inst_no. in (&incl_list) %then %do I = 0 %to 20;
status&i=pxattr&i;
%else %do i = 0 %to 20;
status&i=pxstat&i;
%end;
%mend;
 

 

shellp55
Quartz | Level 8

Thanks for the speedy reply but the same error message is showing up "required operator not found in expression:   &inst_no. in (&incl_list).

Reeza
Super User

Its not 100% clear on what you're doing but I would probably drive the rules using a mapping table of some sort. 

 

Can you illustrate your issue with some sample data?

shellp55
Quartz | Level 8

Reeza, I have many hospitals' data and it is in different formats and some fields are named differently.  I actually have many of the same report per site which is labour intensive when I have to update so I would like one report where I enter the site in a variable and the report produces only for that site, I change the variable and run again.

 

In this instance, the field of CCI status is different for some sites versus others so I wanted to use the same rename macro as always BUT have an if statement as to what field to rename depending on the site.  If it is sites a, b and c then the source field for the status&i = pxattr&i else the source is pxstat&i.  

 

Thanks.

Reeza
Super User

I would say make a table with the variables for each report. 

Then use a portion of your code to select the relevant names from that table. Otherwise you're hard coding the logic which is harder to maintain long term in my experience, especially if you're working with multiple people. 

shellp55
Quartz | Level 8

I am not sure what you are suggesting Reeza.  The data comes from the main DAD data file per site.  At the very least can you advise how to successfully use the in statement and save the variables i.e. with or without quotation marks?  With or without commas?  etc.  Thanks.

Reeza
Super User

You don't need to do that. You can rename lists in a single statement.

 

rename status1-status20 = pxattr1-pxattr20;

So all you need to do, is define the prefix, if the 20 is fixed. 

shellp55
Quartz | Level 8

Interesting, thanks.  I was building off of a previous response on this forum to rename fields for a single site which involved a macro.  Glad to know it can be so much simpler, thanks.

shellp55
Quartz | Level 8

I'm trying to use the if statement with this and I am getting an error of "statement is not valid or it is used out of proper order.  

Reeza
Super User

Post your code and log please.

Reeza
Super User

Something like the following would work - I'm not a fan of macro logic, in case you were wondering. 

 

data _null_;

if &inst_no in (&incl_list) then prefix='pxattr';
else prefix='pxstat';

call symputx('prefix', prefix, 'g');

run;

data have;
set mydata;
rename status1-status20 = &prefix.1-&prefix.20;
run;
art297
Opal | Level 21

While you already have your answer, sometimes macros are the only or best way to accomplish some things. Not saying this is one of those times, but learning how to tame macros is IMHO an extremely important skill. That said, here is one way you could have gotten your macro to work:

data mydata;
  input status0-status20;
  cards;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
;

%let inst_no=2222;
%let incl_list=3333,4444,5555,6666;

%macro renm1/mindelimiter=',';
  %if &inst_no. in &incl_list %then %do i = 0 %to 20;
    status&i=pxattr&i
  %end;

  %else %do i = 0 %to 20;
    status&i=pxstat&i
  %end;
%mend;

options minoperator;
data have;
  set mydata;
  rename %renm1;
run;

Art, CEO, AnalystFinder.com

 

shellp55
Quartz | Level 8

Thank you so much!  "Taming" is a great description and I will certainly tuck this code away for another day.   Thanks for taking the time to respond.  

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