I have a list of code and want to take the first 3 digits or 5 digits of values to assign description. Here is just a short list of code and I have descriptions for 8 codes. Can I use %scan to do the job? I'm trying to use more concise code to do the job. Thanks.
data have; input fc5 $;
datalines;
71234
71333
7149833
;
proc print; run;
%let code= 711 712 713 714 715 717 718 819;
%let desc= Admin IP AC Diagtherap CommHealth Research Education UndAccnt;
data want; set have;
%macro getdesc ();
%do i=1 %to 8;
if substr(fc5,1,3)=%scan(&code,&i) then fc_desc=%scan("&desc.",i);
%end;
%mend;
%getdesc;
run;
And even if you use the double macro variables, you would not need a macro:
%let code= 711 712 713 714 715 717 718 819;
%let desc= Admin IP AC Diagtherap CommHealth Research Education UndAccnt;
data want;
set have;
do i = 1 to 8;
if substr(fc5,1,3) = scan("&code",i) then fc_desc = scan("&desc.",i);
end;
drop i;
run;
But you can clearly see how the format makes the code simple and easy to maintain.
Use a format:
data have;
input fc5 $;
datalines;
71234
71333
7149833
;
run;
data cntlin;
input start :$3. label :$12.;
fmtname = 'myfmt';
type = 'C';
datalines;
711 Admin
712 IP
713 AC
714 Diagtherap
715 CommHealth
717 Research
718 Education
719 UndAccnt
run;
proc format cntlin=cntlin;
run;
data want;
set have;
fc_desc = put(substr(fc5,1,3),$myfmt.);
run;
And even if you use the double macro variables, you would not need a macro:
%let code= 711 712 713 714 715 717 718 819;
%let desc= Admin IP AC Diagtherap CommHealth Research Education UndAccnt;
data want;
set have;
do i = 1 to 8;
if substr(fc5,1,3) = scan("&code",i) then fc_desc = scan("&desc.",i);
end;
drop i;
run;
But you can clearly see how the format makes the code simple and easy to maintain.
Thanks for fixing the code. It worked. Indeed the format will do. The reason I preferred %scan here because I've a long list (longer than the ones shown in my example). and I wanted to use fewer lines to do the job.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.