BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I need to create a format from a part of a string using scan function. I am getting errors. Please suggest.  Thank you

 

%let treat1=Placebo + chemotherapy / Placebo;

proc format;
    value pcrfmt
   1=scan(&treat1.,1,'/')||"Responder"
   2=scan(&treat1.,1,'/')||"Non-Responder"
   
run;
3 REPLIES 3
Tom
Super User Tom
Super User

You cannot run data step code inside the middle of statement inside a PROC. You need to use macro code to generate text that SAS will see as code to be run.

proc format;
  value pcrfmt
   1="%scan(&treat1.,1,/) Responder"
   2="%scan(&treat1.,2,/) Non-Responder"
  ;
run;

You probably do not want those spaces around the / in your TREAT1 macro variable.

%let treat1=Placebo + chemotherapy/Placebo;

If you cannot get rid of them then it might be better to pull the string out first and then use it in the formatted label.

%let treat1=Placebo + chemotherapy / Placebo;
proc format;
  value pcrfmt
%let value=%scan(&treat1.,1,/);
   1="&value. Responder"
%let value=%scan(&treat1.,2,/);
   2="&value. Non-Responder"
  ;
run;

 

Reeza
Super User
I would suggest creating a format using a data set and then using CNTLIN within PROC FORMAT. There's a paper called not just another pretty face - proc format that's a great reference for understanding how to create a format.
Otherwise you could consider having a partial format.

If you really want that to work, use %SCAN() or wrap it in %SYSFUNC()
ballardw
Super User

@knveraraju91 wrote:

Dear,

 

I need to create a format from a part of a string using scan function. I am getting errors. Please suggest.  Thank you

 

%let treat1=Placebo + chemotherapy / Placebo;

proc format;
    value pcrfmt
   1=scan(&treat1.,1,'/')||"Responder"
   2=scan(&treat1.,1,'/')||"Non-Responder"
   
run;

May I ask why you need macro code to build this? If the values "Placebo" and "chemotherapy" are coming from somewhere that you aren't sure what they may be and used to create Treat1 perhaps describing that would be the place to start. Is there a list or something that holds the corresponding "responder" and "non-responder" somewhere? Otherwise this appears way more overkill than is warranted by this example.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 601 views
  • 2 likes
  • 4 in conversation