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

This code lists several variable and intended labels.   However, it won't print all of the fifth value ('Chapter 4'), I assume because of the space.   What is the fix?

 

 

27 %let utilvars=Util_Transp_YN Util_Dental_YN Util_Vision_YN Util_Hearing_YN Util_Ch4_YN Util_Other_YN;
28 %let labvals='Transportation' 'Dental' 'Vision' 'Hearing' 'Chapter 4' 'Other';
29
30 %macro set_labs;
31 %do i=1 %to 5;
32 %put %scan(&utilvars,&i)=%qscan(&labvals,&i);
33 %end;
34 %mend;
35 %set_labs
Util_Transp_YN='Transportation'
Util_Dental_YN='Dental'
Util_Vision_YN='Vision'
Util_Hearing_YN='Hearing'
Util_Ch4_YN='Chapter

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Quotes as part of macro variables may cause any number of syntax issues.

And you've just discovered one of the issues with space delimited data with embedded spaces.

 

An alternate approach:

%let utilvars=Util_Transp_YN Util_Dental_YN Util_Vision_YN Util_Hearing_YN Util_Ch4_YN Util_Other_YN;
%let labvals=Transportation|Dental|Vision|Hearing|Chapter 4|Other;

%macro set_labs;
   %do i=1 %to %sysfunc(countw(&utilvars.));
      %put %scan(&utilvars,&i)="%scan(&labvals,&i,|)";
   %end;
%mend;
%set_labs

Use a specific known delimiter such as the | character. There are reasons involving passing values as parameters that make using commas not as nice. Then use that character as a parameter in the %scan (or %qscan if needed).

Note use of the function %sysfunc to allow the data step function to COUNT the number of words in your Utilvars variable.

 

Depending on how complex a program is you could spend a significant amount of time trying to figure out why "I added variable XXXXX to the list, why is the label not applied?" or "Where is this line " =' ' " coming from" because you don't remember or see that you have fixed iteration in the %do loop and are processing too few or too many values that should be in utilvar.

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Read the documentation.

%put %scan(&utilvars,&i,%str( ),q)=%scan(&labvals,&i,%str( ),q)
ballardw
Super User

Quotes as part of macro variables may cause any number of syntax issues.

And you've just discovered one of the issues with space delimited data with embedded spaces.

 

An alternate approach:

%let utilvars=Util_Transp_YN Util_Dental_YN Util_Vision_YN Util_Hearing_YN Util_Ch4_YN Util_Other_YN;
%let labvals=Transportation|Dental|Vision|Hearing|Chapter 4|Other;

%macro set_labs;
   %do i=1 %to %sysfunc(countw(&utilvars.));
      %put %scan(&utilvars,&i)="%scan(&labvals,&i,|)";
   %end;
%mend;
%set_labs

Use a specific known delimiter such as the | character. There are reasons involving passing values as parameters that make using commas not as nice. Then use that character as a parameter in the %scan (or %qscan if needed).

Note use of the function %sysfunc to allow the data step function to COUNT the number of words in your Utilvars variable.

 

Depending on how complex a program is you could spend a significant amount of time trying to figure out why "I added variable XXXXX to the list, why is the label not applied?" or "Where is this line " =' ' " coming from" because you don't remember or see that you have fixed iteration in the %do loop and are processing too few or too many values that should be in utilvar.

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