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
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.
Read the documentation.
%put %scan(&utilvars,&i,%str( ),q)=%scan(&labvals,&i,%str( ),q)
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.
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!
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.