I have an Excel file with NDCs and Categorys. Here is a sample from it:
| NDC | Category |
| 00536114941 | NSAID |
| 00536308610 | NSAID |
| 00536308641 | NSAID |
| 00536331301 | NSAID |
| 00536331310 | NSAID |
| 00603002622 | NSAID |
| 57881033632 | Opioid |
| 57881033832 | Opioid |
| 00093005001 | Opioid-other |
| 00093015001 | Opioid-other |
| 69344011111 | Opioid-NSAID |
| 69344021111 | Opioid-NSAID |
etc...
I want to create a %LET statement in SAS using the Excel sheet that would do the same thing as:
%LET NSAID = 00536114941 00536308610 00536305641 00536331301 00536331310 00603002622;
%LET Opioid = 57881033632 578810832;
%LET Opioid_other = 00093005001 00093015001;
%LET Opoioid_NSAID = 69344011111 69344021111;
But I want to make it so that if I make changes to the Excel sheet, it will automatically update in SAS--that is I want to use SAS code that creates a %LET statement FROM the Excel file, not copy paste from Excel into SAS.
Figured it out!
Here it is:
data NDCs;
length
ndc $ 11
name $ 15;
set xl.'CPT'n (keep=ndc name)
xl.'ICD_PR'n (keep=ndc name)
xl.'ICD_DX'n (keep=ndc name)
xl.'DRG'n (keep=ndc name)
xl.'NDC'n (keep=ndc name);
where
NDC^= ' ';
run;
libname xl clear;
proc sql;
create table names as
select distinct
name
from NDCs
;quit;
%macro codes(name);
%global &name;
proc sql;
select distinct
quote(strip(code)) into: &name separated by ','
from codes
where
name = "&name"
;quit;
%mend;
data _null_;
set names;
call execute('%codes('||name||');');
run;
Figured it out!
Here it is:
data NDCs;
length
ndc $ 11
name $ 15;
set xl.'CPT'n (keep=ndc name)
xl.'ICD_PR'n (keep=ndc name)
xl.'ICD_DX'n (keep=ndc name)
xl.'DRG'n (keep=ndc name)
xl.'NDC'n (keep=ndc name);
where
NDC^= ' ';
run;
libname xl clear;
proc sql;
create table names as
select distinct
name
from NDCs
;quit;
%macro codes(name);
%global &name;
proc sql;
select distinct
quote(strip(code)) into: &name separated by ','
from codes
where
name = "&name"
;quit;
%mend;
data _null_;
set names;
call execute('%codes('||name||');');
run;
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.