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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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