BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Solph
Pyrite | Level 9

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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;
Kurt_Bremser
Super User

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.

Solph
Pyrite | Level 9

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1047 views
  • 6 likes
  • 2 in conversation