BookmarkSubscribeRSS Feed
KalaBhairava
Quartz | Level 8

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.

10 REPLIES 10
andreas_lds
Jade | Level 19

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?

KalaBhairava
Quartz | Level 8

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.

Kurt_Bremser
Super User

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.

ChrisNZ
Tourmaline | Level 20

 

You probably don't need to do what you are requesting.

Read about the indsname option.

KalaBhairava
Quartz | Level 8

Thanks for your replay Sir.

 

I don't want set  all datasets , it should be separately and macro variable should create in all datasets.

Kurt_Bremser
Super User

@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.

ScottBass
Rhodochrosite | Level 12

@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.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
PeterClemmensen
Tourmaline | Level 20

You don't need a macro. Simply use the Indsname Option in the Set Statement

ScottBass
Rhodochrosite | Level 12

@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.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Reeza
Super User

@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.


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 1196 views
  • 3 likes
  • 7 in conversation