BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Ronein
Meteorite | Level 14

Hello

Let's say that user define a macro var called vector1 that contain values separated by comma.

The task is to create another macro variable called vector2 that contain same values as vector 1 but values separated by space

What is the way to do it please?

%let Vector1='RFA07','RFA02','RFA04','BRG92','PER1903';
/*Task-Create macro variable Vector2 that will be equal to
 'RFA07'   'RFA02'   'RFA04'   'BRG92'   'PER1903'*/

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I messed up the parentheses. Why don't you try to fix them?

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

Use the TRANSLATE function

 

%let Vector2=%sysfunc(translate,&vector1,%str( ),%str(,));
--
Paige Miller
Ronein
Meteorite | Level 14

Thanks,get error

26         %let Vector1='RFA07','RFA02','RFA04','BRG92','PER1903';
27         %let Vector2=%sysfunc(translate,&vector1,%str( ),%str(,));
ERROR: Expected open parenthesis after macro function name not found.

 

PaigeMiller
Diamond | Level 26

I messed up the parentheses. Why don't you try to fix them?

--
Paige Miller
Ronein
Meteorite | Level 14
%let Vector1='RFA07','RFA02','RFA04','BRG92','PER1903';
%let Vector2 = %sysfunc(translate(%quote(&Vector1),' ',','));
%put &Vector1;
%put &Vector2;
PaigeMiller
Diamond | Level 26

@Ronein wrote:
%let Vector1='RFA07','RFA02','RFA04','BRG92','PER1903';
%let Vector2 = %sysfunc(translate(%quote(&Vector1),' ',','));
%put &Vector1;
%put &Vector2;

You want the last two arguments of the TRANSLATE function to be enclosed in %str( ) and not single-quotes. While it works in this case, it may not work in other cases.

 

%let Vector2 = %sysfunc(translate(%quote(&Vector1),%str( ),%str(,)));
--
Paige Miller
tarheel13
Rhodochrosite | Level 12

Try this. 

%let Vector1='RFA07','RFA02','RFA04','BRG92','PER1903';
%let Vector2=%sysfunc(tranwrd(%quote(&vector1),%str(,),%str( )));
%put &=vector1 &=vector2;
 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         %let Vector1='RFA07','RFA02','RFA04','BRG92','PER1903';
 70         %let Vector2=%sysfunc(tranwrd(%quote(&vector1),%str(,),%str( )));
 71         %put &=vector1 &=vector2;
 VECTOR1='RFA07','RFA02','RFA04','BRG92','PER1903' VECTOR2='RFA07' 'RFA02' 'RFA04' 'BRG92' 'PER1903'
 72         
 73         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

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
  • 6 replies
  • 420 views
  • 0 likes
  • 3 in conversation