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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.