Hi everyone,
I want to adjust a macro - here called MacroBL -, which was developed by someone else. For this macro to function, I need to write the variables that I want and separate them with an backslash (\). For each variable I need to write the name, type (CTN: continuous, or CTG: categorical) and label manually. There is no way for this macro to retrieve the variable's format.
What I have tried to do for the last six hours is to let the macro recognize the variable's label, format and type directly as present in the data. I tried several methods like using vlabelx, varlabel or %sysfunc(varlabel) but I didn't get it to work.
I am not a macro expert and the answer should be really easy. Do you guys have an idea? Thanks in forward.
Here is the essential part of the macro I'm trying to manipulate.
%MacroBL(data=, var=var1|CTN|label1\ var2|CTG|label2)
%let nvar=%sysfunc(countw(&var,%str(\)));
%do n=1 %to &nvar;
%let var&n=%scan(%qscan(&var,&n,%str(\)),1,|);
%let vartype&n=%scan(%qscan(&var,&n,%str(\)),2,|);
%let varlabel&n=%scan(%qscan(&var,&n,%str(\)),3,|);
You can get variable names, labels and formats from Dictionary.columns (or sashelp.vcolumn) given a library and dataset name.
Or with proc contents. But are you sure every variable has a format assigned? a Label?
And what information in the data set metadata tells you that a variable is categorical or continuous?
The following code creates a table of properties of the data set SASHELP.BASEBALL.
proc sql; create table work.properties as select name, type,label,format from dictionary.columns where libname='SASHELP' and memname='BASEBALL'; quit;
Note the TYPE will be "char" or "num" in the resulting set. I don't know how to get CTN or CTG from these properties for this data set.
That might be sufficient with a data step using that information to create either an %include file with the call to the macro using that data set information, or Call execute
You can get variable names, labels and formats from Dictionary.columns (or sashelp.vcolumn) given a library and dataset name.
Or with proc contents. But are you sure every variable has a format assigned? a Label?
And what information in the data set metadata tells you that a variable is categorical or continuous?
The following code creates a table of properties of the data set SASHELP.BASEBALL.
proc sql; create table work.properties as select name, type,label,format from dictionary.columns where libname='SASHELP' and memname='BASEBALL'; quit;
Note the TYPE will be "char" or "num" in the resulting set. I don't know how to get CTN or CTG from these properties for this data set.
That might be sufficient with a data step using that information to create either an %include file with the call to the macro using that data set information, or Call execute
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.