BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Tom
Super User Tom
Super User

@Babloo wrote:

@Patrick  two questions a) How to tweak your solution without creating the new macro variable, 'parameter_new'?  b) In the below code block, you've hardcoded the values but in real life the values of macro variable will change and it can have any special/invisible characters.  

 

%let parameter=Data Management,Operations,Sales ;

/* construct value for macro variable parameter that includes blanks, a tab and a line feed */
data _null_;
  call symput('parameter',cat('Data','0A'x,'    Management ,  ','09'x,'  Operations , Sales  '));
run;

On a separate note, I'd like to know how to handle if we have multibyte characters or single quotes in the macro variable values.


That data step was just included to CREATE AN EXAMPLE VALUE OF PARAMETER.

You will not need or want to include it in you actual program.  But you can use it to create a value of PARAMETER that has some special characters so you can TEST your other logic.

Babloo
Rhodochrosite | Level 12
Thank you for answering one of my questions. How to tweak the second data
_null_ step without creating the new macro variable 'parameter_new'?
Tom
Super User Tom
Super User

@Babloo wrote:
Thank you for answering one of my questions. How to tweak the second data
_null_ step without creating the new macro variable 'parameter_new'?

Tweak it to do WHAT exactly?  The code is well commented.

 

data _null_;
  length string $1000;
  /* replace all whitespace characters with a blank */
  string=prxchange('s/\s+/ /',-1,strip("&parameter"));
  /* remove all blanks before or after a comma and add quotes before and after a comma */
  string=prxchange("s/ *(,) */','/",-1,strip(string));
  /* create new macro varialbe, add quotes at the start and end of the string */
  call symputx('parameter_new',cats("'",string,"'"));
run;

%put parameter_new: %nrbquote(&parameter_new);

 

 

What part of you do you want to change? In what way?

 

The first argument to CALL SYMPUTX() is the NAME of the macro variable to be written. If you want to write into a different macro variable than change the name being given to CALL SYMPUTX().

 

 

Patrick
Opal | Level 21

@Babloo wrote:

@Patrick  two questions a) How to tweak your solution without creating the new macro variable, 'parameter_new'?  b) In the below code block, you've hardcoded the values but in real life the values of macro variable will change and it can have any special/invisible characters.  

 

%let parameter=Data Management,Operations,Sales ;

/* construct value for macro variable parameter that includes blanks, a tab and a line feed */
data _null_;
  call symput('parameter',cat('Data','0A'x,'    Management ,  ','09'x,'  Operations , Sales  '));
run;

On a separate note, I'd like to know how to handle if we have multibyte characters or single quotes in the macro variable values.


In above block I'm creating and populating macro variable parameter with some extra spaces, a tab and an line feed. That's the "Have" to mimic what you describe you're dealing with.

 

The 2nd data step then replaces any invisible character to a blank.  The \s meta character in the RegEx is used for this.

 

If you have multibyte characters then we can't use RegEx that in the SAS implementation only works for single byte character sets. So would need to code this differently. 

Dealing with single quotes would be possible with a bit more extra logic.

 

...trying to provide you a solution to a real problem so not really motivated to spend time for a fully generic approach. 

 

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 48 replies
  • 3862 views
  • 31 likes
  • 11 in conversation