BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Junyong
Pyrite | Level 9

Suppose I want to open a text file have.txt using notepad.exe in SAS. Then, the code is

x notepad c:\users\junyong\desktop\have.txt;

However, I found that using environment variables (percent signs) as follows causes an error—not in SAS but in notepad.exe.

x notepad %userprofile%\desktop\have.txt;

What should I do to properly pass the percent signs and, therefore, the environment variables in this case?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Try using the %SYSGET() macro, or SYSGET() function to find the value of USERPROFILE.

First check if USERPROFILE exists and what value it has.

%put Length of USERPROFILE is $sysfunc(envlen(USERPROFILE)).;
%put Value of USERPROFILE is %sysget(USERPROFILE).;

Then try using it in your X command:

x notepad %sysget(USERPROFILE)\desktop\have.txt;
x "notepad %sysget(USERPROFILE)\desktop\have.txt";

Also why are you running NOTEPAD on the file?  If you want to read why not just read it?  If you want to edit it why not just edit it in the program editor (or the "enhanced" program editor) of Display Manager?

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

Not sure I understand what error you are having.  How is it causing trouble for Notepad?  Does the environment variable not get resolved?  Are you sure the environment variable exists?  Does the value of the environment variable contain spaces?

 

Try adding double quotes for Windows to recognize the name as a single string.  Also add single quotes to prevent SAS from trying to execute a macro named USERPROFILE.

 

x 'notepad "%userprofile%\desktop\have.txt"';

 

Junyong
Pyrite | Level 9

%userprofile% surely exists and is a Windows environment variable. It contains no space currently—c:\users\junyong\.

The first code—with no percent sign—executes notepad.exe and opens c:\users\junyong\desktop\have.txt correctly.

I want to shorten the full path using the existing Windows environment variable %userprofile% (therefore, %userprofile%\desktop\have.txt).

The second code—with percent signs—executes notepad.exe but fails to find c:\users\junyong\desktop\have.txt.

Instead, if there exists c:\program files\sashome\sasfoundation\9.4\%userprofile%\desktop\have.txt, which is weird, then the notepad.exe opens it.

How can I make SAS pass the correct path to notepad.exe rather than the weird path using X statements?

I tried your suggestion with quotation marks but failed to open the file again.

Tom
Super User Tom
Super User

Try using the %SYSGET() macro, or SYSGET() function to find the value of USERPROFILE.

First check if USERPROFILE exists and what value it has.

%put Length of USERPROFILE is $sysfunc(envlen(USERPROFILE)).;
%put Value of USERPROFILE is %sysget(USERPROFILE).;

Then try using it in your X command:

x notepad %sysget(USERPROFILE)\desktop\have.txt;
x "notepad %sysget(USERPROFILE)\desktop\have.txt";

Also why are you running NOTEPAD on the file?  If you want to read why not just read it?  If you want to edit it why not just edit it in the program editor (or the "enhanced" program editor) of Display Manager?

Junyong
Pyrite | Level 9

Thanks—resolving the environment variable in SAS beforehand worked. I am actually using a external software other than notepad.exe, but my problem was related to the environment variable issue. I posted this as a minimum working example because notepad.exe is common.

Tom
Super User Tom
Super User

@Junyong wrote:

Thanks—resolving the environment variable in SAS beforehand worked. I am actually using a external software other than notepad.exe, but my problem was related to the environment variable issue. I posted this as a minimum working example because notepad.exe is common.


I highly recommend using the PIPE method as posted by @Kurt_Bremser .  That way your SAS program can see any messages generated by the program and/or the operating system.

Kurt_Bremser
Super User

Looks like the shell used by the X statement does not resolve the userprofile environment variable.

Run zhe command with a filename pipe and stderr redirection:

filename oscmd pipe 'notepad %userprofile%\desktop\have.txt 2>&1';

data _null_;
infile oscmd;
input;
put _infile_;
run;

and see if the log provides a clue.

Junyong
Pyrite | Level 9

Unlike x, pipe works well. For example,

data _null_;
	infile "notepad %userprofile%\desktop\asdf.txt" pipe;
run;

executes notepad.exe and opens the asdf.txt correctly.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1441 views
  • 2 likes
  • 3 in conversation