BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ksharp
Super User

Combine it into data step :

%macro eia_md5hex32/parmbuff;

%let list=;

%let n=%sysfunc(countw( %bquote(&syspbuff) , %str(,) ));

%do i=1 %to &n;

  %let a&i= %qscan(%bquote(&syspbuff),&i,%str(,%(%)) );

  %let list= &list.,a&i ;

%end;

  %let list=%substr(%bquote(&list),2);

  %syscall sortc(

   &list

  );

  %put  &list  %bquote(&a1 &a2 &a3) ;

  %global sorted_string;

  %let sorted_string=;

  %do i=1 %to &n;

  %let sorted_string= &sorted_string.%bquote(&&a&i) ;

  %end;

  %put sorted_string: %bquote(&sorted_string) ;

   put(md5(symget('sorted_string')), $hex32.)

%mend;

data _null_;

  set sashelp.class;

  key1=%eia_md5hex32(age,'x',height);

  key2=%eia_md5hex32(height,'x',age);

  put key1=;

  put key2=;

  stop;

run;

Xia Keshan

Message was edited by: xia keshan

data_null__
Jade | Level 19

Would it be a good idea to add %LOCAL to insure those variables you're creating don't escape and cause havoc?  Would know to do that?

data_null__
Jade | Level 19

Yes I see, I've looked back at your original post and see that you did include %LOCAL.  I don't think you would need to use _ as first character unless you just like that.  I usually save the underscores for data steps where I want to use names that I am reasonably sure won't exist in the users data.

Patrick
Opal | Level 21

I know I sometimes exaggerate it a bit with the underscore. Like you I use it as an indicator that variables are "temporary".

Tom
Super User Tom
Super User

So combining the ideas posted is sounds like you want a utility macro that will take as input a list of variable names and return the same names in sorted order.

%macro sort_names /parmbuff;

  %local i n names result sep;

  %let n=%sysfunc(countw(&syspbuff,(, )));

  %do i=1 %to &n;

    %local v&i;

    %let v&i=%upcase(%scan(&syspbuff,&i,(, )));

    %let names=&names.&sep.v&i;

    %let sep=,;

  %end;

  %syscall sortc(&names);

  %let sep=;

  %do i=1 %to &n;

    %let result=&result.&sep.&&v&i;

    %let sep=,;

  %end;

&result

%mend ;

%put %sort_names(a,B,C);

%put %sort_names(x1, x2 ,B,C);


You could then use that as input to CATX() or other functions to generate you unique string.


data _null_;

  set sashelp.class;

  key1=catx('|',%sort_names(age,'x',height));

  key2=catx('|',%sort_names(height,age,'x'));

  put key1= / key2= ;

  stop;

run;


key1=X|14|69

key2=X|14|69

Patrick
Opal | Level 21

Thank you. Unfortunately I can only mark one answer as correct so I've picked the first which gave me a convincing solution.

slchen
Lapis Lazuli | Level 10

This is also one of methods.

%macro sort_names/parmbuff;

   filename clip clipbrd;

   data _null_;

   file clip dlm=',';

   put "&syspbuff";

   data vars;

   infile clip dlm=',';

   input name $ @@;

   name=compress(name,'()');

   run;

   proc sql;

   select name into:result separated by ',' from vars order by name;

   quit;

   %let result=(&result);

%mend;

%put %sort_names(x,a,d);

%put %sort_names(x1, x2 ,B,C);

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 22 replies
  • 4772 views
  • 6 likes
  • 8 in conversation