Hi Every one,
i have 10 data sets (AA,BB,CC,DD,EE,FF,GG,HH) in one library and i want create macro to convert data set name into variable name (New ).
for ex: for AA data set i want variable New =AA
for BB data set i want variable New =BB;
please give me a solution.
Sounds strange, that you want a variable in a dataset with the same name as the dataset. What do you want to do with that variable? What type and length should it have?
Hi Sir,
for CDISC studies(Clinical trails) we have to create variable Domin and it should contain data set name. and length also minum $2 and max 5.
As already mentioned, use the indsname= option:
data want;
set
aa
bb
cc
dd
ee
ff
gg
hh
indsname=inds
;
length domin $5;
domin = inds;
run;
Be aware that restricting the length of SAS dataset names may be up to 32, so restricting it to 5 is a disaster waiting to happen. Someday someone will apply your macro to a bunch of datasets with longer names.
You probably don't need to do what you are requesting.
Read about the indsname option.
Thanks for your replay Sir.
I don't want set all datasets , it should be separately and macro variable should create in all datasets.
@KalaBhairava wrote:
Thanks for your replay Sir.
I don't want set all datasets , it should be separately and macro variable should create in all datasets.
Macro variables exist outside datasets, so this is not possible.
If you have trouble describing what you want, supply example data and show the expected outcome.
@KalaBhairava wrote:
Thanks for your replay Sir.
I don't want set all datasets , it should be separately and macro variable should create in all datasets.
Huh? This makes no sense.
I promise I'm not trolling. But if English isn't your native language, do you have a colleague who can help you clearly state what you are trying to do? I think that will help a lot in us helping you better.
You don't need a macro. Simply use the Indsname Option in the Set Statement.
@KalaBhairava wrote:
Hi Every one,
i have 10 data sets (AA,BB,CC,DD,EE,FF,GG,HH) in one library and i want create macro to convert data set name into variable name (New ).
for ex: for AA data set i want variable New =AA
for BB data set i want variable New =BB;
please give me a solution.
That's only 8 datasets.
Define "variable". We need more context. What do you really need to do?
data aa bb cc dd ee ff gg hh ii jj;
x=1;
run;
* delete the _PRODSAVAIL dataset ;
proc datasets lib=work kill nowarn nolist;
quit;
proc sql noprint;
select catx('.',libname,memname) into :datasets separated by ' '
from dictionary.tables
where libname='WORK';
quit;
%put &=datasets;
%macro code;
%let new=&word;
%put &=new;
%mend;
%loop(&datasets)
Add https://github.com/scottbass/SAS/blob/master/Macro/loop.sas to your macro autocall library.
Change:
select catx('.',libname,memname) into :datasets separated by ' '
to
select memname into :datasets separated by ' '
if you just want the member name only.
@KalaBhairava wrote:
Hi Every one,
i have 10 data sets (AA,BB,CC,DD,EE,FF,GG,HH) in one library and i want create macro to convert data set name into variable name (New ).
for ex: for AA data set i want variable New =AA
for BB data set i want variable New =BB;
please give me a solution.
Would you walk up to someone and say "please give me a solution"?
%macro addName(indsn=, outdsn=, varName=);
data &outdsn.;
set &indsn;
&varName = "&indsn.";
run;
%mend;
%addName(indsn=AA, outdsn=AA_New, varName=);
Now to call it for all data sets in a library, use CALL EXECUTE and SASHELP.VTABLE.
data demo; set sashelp.vtable; where libname = 'CDISCXX'; *can add filtering conditions here; str = catt('%addName(indsn=', memname, ', outdsn=', catx("_", memname, "new"), ", varName=New", ");" ); call execute(str); run;
Make sure str is being calculated correctly after you've test it.
In general, first make sure your base code is working and then you can convert any program into a macro using this approach.
Good Luck.
https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
PS. "please give me a solution" isn't something most people would ever say, it's more likely you intended to say "please help me with this problem", because this isn't a coding service where you're entitled to a solution or someone will write the code for you, this is a community of volunteers who you're asking for help. I'm going to assume that this is a just a translation issue.
@KalaBhairava wrote:
Hi Every one,
i have 10 data sets (AA,BB,CC,DD,EE,FF,GG,HH) in one library and i want create macro to convert data set name into variable name (New ).
for ex: for AA data set i want variable New =AA
for BB data set i want variable New =BB;
please give me a solution.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.