BookmarkSubscribeRSS Feed
JMartin24
Calcite | Level 5

Hello. I am looking for a way to automate a password generation script I have to run very often. I basically need to insert some text, run pwencode, insert more text.  Any help would be greatly appreciated.

 

EX:

text 1: %LET PASS =%STR(

pwencode

text2: );

 

8 REPLIES 8
Quentin
Super User

I'm having a hard time understanding your question.  Can you explain a bit more of your goal, and add to your example?

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
JMartin24
Calcite | Level 5

Absolutely. I hope the below is more clarifying

 

I would want to be able to run a script/macro with the input to be:

 

text 1: %LET PASS =%STR(

 

FILENAME PW '\\Directory\\MYPASS.SAS'

PROC PWENCODE

IN = 'Password'

OUT = PW;

RUN;

 

text2: );

 

With the ouput to be a singular sas file that contains:

%LET PASS =%STR(EncryptedPasswordStringFromPWENCODE);

Quentin
Super User

Sorry, I'm still not following.  I don't understand what text2 represents.  

 

If your goal is to create a macro variable that has the value of the encoded password, PROC PWENCODE does that automatically.  It creates a global macro variable, _PWENCODE. So you can do:

 

proc pwencode in='mypassword' ;
run ;
%let pass=&_PWencode ;

And that will store the encoded password in the macro variable PASS.

 

If you want to create function that will return an encoded password, you could use DOSUBL to create a function-like macro that returns an encoded password, e.g.:

%macro pwencode(password);
  %local rc ;
  %let rc=%sysfunc(dosubl(%nrstr(
    proc pwencode in="&password" ;
    run ;
  )));
  &_PWencode
%mend;

%let mypass=%pwencode(mypassword) ;
%put &=mypass ;

%let yourpass=%pwencode(yourpassword) ;
%put &=yourpass ;

So if the goal is to get an encoded password string into a macro variable, you don't need to generate a .sas file with a %LET statement in it.

 

Does that help?

 

EDIT:

In the macro, I added _PWENCODE to the %local statement. This avoids the macro variable _PWENCODE from being written to the global symbol table of your main session.  That said, I'm not necessarily recommending the DOSUBL approach.

 

Another Edit:

Removed _PWENCODE from the %local statement, cuz it broke stuff. : ) Apparently when PROC PWENCODE runs in the DOSUBL side-session, it still creates the macro variable _PWENCODE in the main session global table. Unfortunate.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Tom
Super User Tom
Super User

Making a LOCAL macro variable does not work.  It just hides the value of the of the GLOBAL macro variable that the PROC creates.

Quentin
Super User

@Tom wrote:

Making a LOCAL macro variable does not work.  It just hides the value of the of the GLOBAL macro variable that the PROC creates.


Thanks Tom.  I had assumed that PROC PWENCODE executed in the DOSUBL side-session would write to the side-session global symbol table, allowing it to be returned to the main session local variable.  But sadly, looks like that is not the case.  I should have tested more.  Edited the code.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Quentin
Super User

@Tom wrote:

Making a LOCAL macro variable does not work.  It just hides the value of the of the GLOBAL macro variable that the PROC creates.


Hi Again @Tom  et al,

 

This has been bugging me off-an-on during the day, because I remember I did get my DOSUBL function-style macro working like I wanted at one point this morning before posting.  Now at the end of the day I came back to it, and realized the DOSUBL problem I hit. 

 

If you code:

%macro pwencode(password);
  %local rc _pwencode;
  %let rc=%sysfunc(dosubl(%nrstr(
    proc pwencode in="&password" ;
    run ;
  )));
  &_PWencode
%mend;

%let mypass=%pwencode(mypassword) ;
%put _user_ ;

By my understanding of the rules of DOSUBL magic, it 'should' work.  I think PROC PWENCODE 'should' create the macro variable _PWENCODE in the side-session global symbol table, and then return the value of the macro variable to the main session local symbol table when DOSUBL completes.  But that does not happen, instead PROC PWENCODE creates the macro variable _PWENCODE in the main session global symbol table.  So this code does not work.

 

But, if you use a %LET statement in the DOSUBL block to create a global macro variable named _PWENCODE in the side session before calling PROC PWENCODE, then PROC PWENCODE will write to that macro variable.  And this allows the function-style macro to work like I had intended.  It avoids creating a global macro variable in the main session symbol table.

 

%macro pwencode(password);
  %local rc _pwencode; 
  %let rc=%sysfunc(dosubl(%nrstr(
    %let _pwencode= ; %*Create global macro variable in side session, PROC PWENCODE will assign it a value ;
    proc pwencode in="&password" ;
    run ;
  )));
  &_PWencode
%mend;

%let mypass1=%pwencode(aaa) ;
%let mypass2=%pwencode(bbb) ;
%put _user_ ;

 

 

 

 

 

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
Tom
Super User Tom
Super User

You have not parametrized your inputs efficiently.

Sounds like your output is a file.

So your inputs are a filename and a password (a string).

%macro pwencode(password,filename);
filename pwencode temp;
proc pwencode in="&password" out=pwencode;
run;
filename pwencode;
data _null_;
  file "&filename";
  put '%let pass=' "&_PWencode" ';' ;
run;
%symdel _pwencode;
%mend pwencode ;

Let's try it:

filename test temp;
%pwencode(password=mypass,filename=%sysfunc(pathname(test)))

Result:

%let pass={SAS002}E2B3D742098C5EEE300EF38F;
rudfaden
Lapis Lazuli | Level 10

You can try this

filename pw temp;

proc pwencode in='mypass1' out=pw;
run;

data _null_;
    infile pw truncover;
    input pws $char100.;
    file print;
    call symput('password',pws);
run;

%put &=password;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1638 views
  • 10 likes
  • 4 in conversation