BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
KeanuHua4
Calcite | Level 5

I would like my code to be able to run out of the box when downloaded from my GitHub portfolio (this is just the standard for the other 2 coding languages on it). However, while normal SAS files will let me set the current working directory by clicking the SAS file,  this is not the case with the sasnb files on VS Code. As such, sasnb files can not run out of the box.

 

System notes: Windows 11, SAS on VS Code was last updated 2025-02-05.

 

Observation:

SAS will run this whenever I run code:

 

%let _SASPROGRAMFILE = %nrquote(%nrstr(c:\Users\3sekk\Desktop\spring 25\stat 574\hw1STAT574S25\01\01.sasnb));

 

Attempted solution: I can use the above as &_SASPROGRAMFILE. I have tried to extract c:\Users\3sekk\Desktop\spring 25\stat 574\hw1STAT574S25\01 so that I can send this to a rc=dlgcdir step, but I haven't managed to figure it out. I have found that the regex code ^(.+)\\ will highlight that, and its output will suffice for the rc=dlgcdir step. The issue is regexing the &_SASPROGRAMFILE.

 

That is just one attempted solution. Is there a simpler way?

 

Stat nation!
1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

In a SAS session for VS Code, the value of the _SASFILEPATH macro variable is automatically set when you run code from a saved program file. You can extract the path portion of a fully qualified Windows file name like this:

%let filepath=%qsubstr(%superq(_SASPROGRAMFILE),1,%sysevalf(%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))-1));

This part of the code reads backward from the end of the string, and finds the location of the first '\' character:
%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))

The next portion takes that value and subtracts 1:

%sysevalf(%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))-1)

Now we know the location of the end of the path in the text, so we use %QSUBSTR to extract the text from position 1 to the calculated end position:

%qsubstr(%superq(_SASPROGRAMFILE),1,%sysevalf(%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))-1))

We can execute a %PUT statement to see the value in the log:

%put NOTE: Path is %superq(filepath);

Which yields:

NOTE: Path is c:\Users\3sekk\Desktop\spring 25\stat 574\hw1STAT574S25\01

 

Check out my Jedi SAS Tricks for SAS Users

View solution in original post

2 REPLIES 2
SASJedi
SAS Super FREQ

In a SAS session for VS Code, the value of the _SASFILEPATH macro variable is automatically set when you run code from a saved program file. You can extract the path portion of a fully qualified Windows file name like this:

%let filepath=%qsubstr(%superq(_SASPROGRAMFILE),1,%sysevalf(%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))-1));

This part of the code reads backward from the end of the string, and finds the location of the first '\' character:
%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))

The next portion takes that value and subtracts 1:

%sysevalf(%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))-1)

Now we know the location of the end of the path in the text, so we use %QSUBSTR to extract the text from position 1 to the calculated end position:

%qsubstr(%superq(_SASPROGRAMFILE),1,%sysevalf(%sysfunc(find(%superq(_SASPROGRAMFILE),\,-99999))-1))

We can execute a %PUT statement to see the value in the log:

%put NOTE: Path is %superq(filepath);

Which yields:

NOTE: Path is c:\Users\3sekk\Desktop\spring 25\stat 574\hw1STAT574S25\01

 

Check out my Jedi SAS Tricks for SAS Users
Tom
Super User Tom
Super User

Can you explain in more detail what you are doing?

Are you asking how to remove the filename part from a full qualified filename so that you just have the directory name?  You should be able to use FINDC() and SUBSTRN() easily enough.

%let _SASPROGRAMFILE = %nrquote(%nrstr(c:\Users\3sekk\Desktop\spring 25\stat 574\hw1STAT574S25\01\01.sasnb));

data _null_;
  length dirname $256;
  loc = findc(symget('_SASPROGRAMFILE'),'/\',-9999);
  dirname=substrn(symget('_SASPROGRAMFILE'),1,loc);
  put dirname=:$quote.;
run;

So once you have the directory name you could pass it to DLGCDIR() to change current directory.

Assuming that the directory actually exists on the machine where SAS itself is running.

107  %let _SASPROGRAMFILE = %nrquote(%nrstr(c:\Users\3sekk\Desktop\spring
107! 25\stat 574\hw1STAT574S25\01\01.sasnb));
108
109  data _null_;
110    length dirname $256;
111    loc = findc(symget('_SASPROGRAMFILE'),'/\',-9999);
112    dirname=substrn(symget('_SASPROGRAMFILE'),1,loc);
113    if fileexist(dirname) then rc=dlgcdir(dirname);
114    else put 'NOTE: ' dirname= 'does not exist.';
115  run;

NOTE: dirname=c:\Users\3sekk\Desktop\spring 25\stat 574\hw1STAT574S25\01\
does not exist.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

Is the SAS session you are using VSCODE to submit SAS code to running on the same machine as the machine that is running VSCODE?

 

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 635 views
  • 1 like
  • 3 in conversation