Generate SAS dataset names (like in SAS DI)

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Generate SAS dataset names (like in SAS DI)

Hi,

I have a requirement to generate SAS dataset names in my SAS code like the ones gets generated automatically in SAS DI - (e.g - work.W5T9JQ6B, work.W5T9J46Q, work.W5T9J46X).

Can someone suggest how to do this in SAS or any OOPS language.

Regards

Dhana


Accepted Solutions
Solution
‎08-05-2014 04:37 AM
SAS Super FREQ
Posts: 708

Re: Generate SAS dataset names (like in SAS DI)

hi

the names used by SAS Data Integration Studio are Metadata ID's, If you simply need to create a unique name you could create a macro like below:

%macro createName(prefix=A);
 
%local indexName ts  ;
  %let ts = %sysevalf( %sysfunc( datetime() ) * 1000 + &sysindex);
  %let indexName = &prefix%sysfunc( putn(&ts, hex12.) );
  %put NOTE: &sysMacroname &indexName used;
  &indexName
%mend;

%macro test_createName;
 
%local i;
  %do i = 1 %to 10;
   
%put NOTE: *%createName*;
  %end;
%mend;

options nomprint nomlogic;
%
test_createName

View solution in original post


All Replies
Super Contributor
Posts: 340

Re: Generate SAS dataset names (like in SAS DI)

You could use proc datasets:

Proc Datasets Library=Work MemType=Data /*NoList*/;

  Contents Out=Work_Cont (Keep=MemName) Data=_ALL_ /*NoPrint*/;

Run;

Contributor dkb
Contributor
Posts: 53

Re: Generate SAS dataset names (like in SAS DI)

Keep a counter (in a macro variable or a utility table) and every time you create a new table, increment the counter by 1.  As long as your counter stays below 36 ** 7, the following will convert an integer into a base 36 number of seven "digits" (but you might like to add a check for overflow):

CODE:

%let counter = 99999999;

data _null_;

  length string $ 7 char $ 1 tablename $ 8;

  num = input(symget('COUNTER'), 16.);

  do i = 1 to 7;

  remain = mod(num, 36);

  char = substr('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', remain+1, 1);

  string = char !! left(string);

  num = int(num / 36);

  end;

  tablename = 'W'!!string;

  call symput('TABLE', tablename);

run;

%put &TABLE.;

LOG:

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.01 seconds

     

28        

29         %put &TABLE.;

W01NJCHR

Solution
‎08-05-2014 04:37 AM
SAS Super FREQ
Posts: 708

Re: Generate SAS dataset names (like in SAS DI)

hi

the names used by SAS Data Integration Studio are Metadata ID's, If you simply need to create a unique name you could create a macro like below:

%macro createName(prefix=A);
 
%local indexName ts  ;
  %let ts = %sysevalf( %sysfunc( datetime() ) * 1000 + &sysindex);
  %let indexName = &prefix%sysfunc( putn(&ts, hex12.) );
  %put NOTE: &sysMacroname &indexName used;
  &indexName
%mend;

%macro test_createName;
 
%local i;
  %do i = 1 %to 10;
   
%put NOTE: *%createName*;
  %end;
%mend;

options nomprint nomlogic;
%
test_createName
Respected Advisor
Posts: 4,173

Re: Generate SAS dataset names (like in SAS DI)

That's a weird requirement and I'm curious what the purpose of it is.

If this is about automatically generating SAS Work table names and make sure that the names are unique then you could also let SAS do it using below syntax:

data;

  set sashelp.class;

run;

%put &syslast;

data;

  set &syslast;

run;

%put &syslast;

Super User
Posts: 10,020

Re: Generate SAS dataset names (like in SAS DI)

UUID function :

%let dsname=w%scan(%sysfunc(uuidgen()),1);

%put &dsname ;

Xia Keshan

Contributor dkb
Contributor
Posts: 53

Re: Generate SAS dataset names (like in SAS DI)

Thanks Ksharp, the uuidgen function is new to me.

Frequent Contributor
Posts: 75

Re: Generate SAS dataset names (like in SAS DI)

Thank you everyone...! The code provided by Bruno Muller & Ksharp soved my problem.

Regards

Dhana

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 394 views
  • 4 likes
  • 6 in conversation