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


Hi Chris,

I am trying to study and use your SAS Harness, but find out that the SAS workspace server only allow me to connect through SAS EG. When I try to connect to workspace server using SAS Harness, it give me error mesage saying I don't have permission. My question is can I use COM protocol to connect to our SAS Unix server instead of workspace server (that is set on a different unix node). If it can be done, do you have an example? This is more useful, because all our data and programs are on this SAS unix server.

Thanks!

Yihong

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

Yihong, sorry for the delay on this.  Here's a C# example of copying a file from "SASApp" to the local PC.  This example has several hardcoded values, so you'll need to adapt for your own situation.

using SAS.Tasks.Toolkit;

/* ... */

// initialize the path of the source file on the SAS server

// ex: '/u/myid/data/test.csv'

string filename = txtFile.Text;

// assuming filename ends with extension

// results in 'csv', for example

string ext = filename.Split('.')[filename.Split('.').Length - 1];

// get an object that represents the SAS server session

SasServer s = new SasServer("SASApp");

// copies to a local TEMP folder, returns the full path where

// it was copied to

string dest = s.CopyServerFileToLocal(filename, "test", ext);

MessageBox.Show("Copied file to " + dest);

You'll see a message like:

   Copied file to C:\Users\sascrh\AppData\Local\Temp\SEG1896\ekecsy3k.2hj\test.csv

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

View solution in original post

18 REPLIES 18
yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

Through Object Broswer, I saw the following. They all need a local SAS server, right? So for SAS EG that has no local server, they cannot be used?

SAS.Tasks.Toolkit.SasServer.CopyLocalFileToServer(string)

SAS.Tasks.Toolkit.SasServer.CopyServerFileToLocal(string, string, string)

SAS.Tasks.Toolkit.SasServer.CopyServerFileToLocal(string, string)

Or ask the question in a different way. do you have examples to do the following for SAS EG on Unix server without local server:

1. Transfer a local text file to Unix.

2. Transfer a unix text file to Local.

3. Transfer a SAS dataset from Unix to Local.

4. Transfer a SAS dataset from Local to Unix.

These will be very helpful for me to start.

Thanks!

ChrisHemedinger
Community Manager

Yihong,

In this case, the "Local" in these method names is "local PC" -- no Local SAS is required.  I'll try to come up with a simple example that you can use.

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

That will be great!

Thanks!

Yihong

yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

Hi Chris,

While waiting for the file and data transfer examples, I have another question.

In the following is the code in your book page 64. I get erroes as shoown in the mouse on message shown.

I do have SAS.Tasks.Toolkit reference added and have using SAS.Tasks.Toolkit; and using SAS.Tasks.Toolkit.Data; on top of my program. Anything elsse I am missing?

Also, in the TopN example, I can not find this piece of code to see how it is used. Where can I find it in the TopN example?

Thakns!

Yihong

File_Dialog_Question.png

ChrisHemedinger
Community Manager

Yihong,

Add these using directives to the top of your file:

using System.Collections.ObjectModel;

using System.Collections.Generic;

You can always right-click on the "offending" keyword and select Resolve to see if Visual Studio has any suggestions for you.

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
ChrisHemedinger
Community Manager

Yihong, sorry for the delay on this.  Here's a C# example of copying a file from "SASApp" to the local PC.  This example has several hardcoded values, so you'll need to adapt for your own situation.

using SAS.Tasks.Toolkit;

/* ... */

// initialize the path of the source file on the SAS server

// ex: '/u/myid/data/test.csv'

string filename = txtFile.Text;

// assuming filename ends with extension

// results in 'csv', for example

string ext = filename.Split('.')[filename.Split('.').Length - 1];

// get an object that represents the SAS server session

SasServer s = new SasServer("SASApp");

// copies to a local TEMP folder, returns the full path where

// it was copied to

string dest = s.CopyServerFileToLocal(filename, "test", ext);

MessageBox.Show("Copied file to " + dest);

You'll see a message like:

   Copied file to C:\Users\sascrh\AppData\Local\Temp\SEG1896\ekecsy3k.2hj\test.csv

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

Hi Chris,

This is a wonderful way of transfer file from server to local. Very handy to use and much easier than I expected.

Thank you very much for this example!

Yihong

yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

Hi Chris,

Have another question. In the Training book, page 4-11 (as shown in the attached), can you send me the more specific link to the Task API and Toolkit Documentation. From the link: http://go.sas.com/customtasksapi I cannot find what it is shown in the slide.

Thanks!

Yihong

ChrisHemedinger
Community Manager

Yihong,

It's the Reference Documentation link from that page.  It's a ZIP file with a CHM (Microsoft Help file) and some PDFs.  The CHM file has the reference info.  After extracting it from the ZIP file,  you might need to right-click on the CHM file, choose Properties and select Unblock in the security settings.  Microsoft Windows often blocks CHM content from "untrusted" sources (the Internet).

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

Thank you very much Chris! This is very helpful!

Yihong

yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

Hi Chris,

I have a question about reading the SAS Macro variable. See the following code. I have a method getMacro(string varName) from line 9. In line 1, I call the method to read AUTOMATIC macro variable SYSUSERID. It alwars works. In line 7, I call the same method again to read GLOBAL macro variable SASWORKLOCATION. It is not working. But if I add a submitter as shown in line 3 to line 5, it will work. It works for my purpose, but I like to know why it behaves like this. Can you help explain?

Thanks!

Yihong

============================================================================

        public string userID = "";

        public string wkg = "";

1        userID = getMacro("SYSUSERID"); MessageBox.Show(userID);

3        SAS.Tasks.Toolkit.SasSubmitter submitter = new SAS.Tasks.Toolkit.SasSubmitter("SASApp");

4        string log;

5        submitter.SubmitSasProgramAndWait("%let wkc = %sysfunc(getoption(work));", out log);

6

7        wkg = getMacro("SASWORKLOCATION");

8

9        public string getMacro(string varName)

10       {

11          // Consumer.AssignedServer is the active server from SAS EG

12          SasServer s = new SasServer(Consumer.AssignedServer);

13          string vName = s.GetSasMacroValue(varName); MessageBox.Show(vName);

14          return vName;

15       }

ChrisHemedinger
Community Manager

Yihong,

The macro variables that SAS Enterprise Guide creates when it submits a program/task are also "cleared" at the end of the task.  You'll see the following code added to each task/program in the log:

%LET _CLIENTTASKLABEL=;

%LET _CLIENTPROJECTPATH=;

%LET _CLIENTPROJECTNAME=;

%LET _SASPROGRAMFILE=;


This means that these macro variables are NOT defined when you check their values "in between" tasks.


Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
yhz
Fluorite | Level 6 yhz
Fluorite | Level 6

But macro variable SASWORKLOCATION is not cleared, right? Whe I check its value with %put &SASWORKLOCATION, it is always there in the SAS EG session, before or after I run the customer task.

Also, when I add the line 3 - 5 in my code (a submitter), the line 7 ( wkg = getMacro("SASWORKLOCATION"); ) works. Why submit a line of SAS code in the middle makes it works?
Thanks!
Yihong
ChrisHemedinger
Community Manager

Yihong,

Ah, I see what you mean.  The GetSasMacroValue is actually performing a data query, something like this:

select * from
  sashelp.vmacro
where name="SASWORKLOCATION";

I'm not sure why it would behave differently -- except this: the SASWORKLOCATION macro variable is not initialized until after the first program or task is submitted from SAS Enterprise Guide.  So if you start EG then immediately run your custom task, which then queries this value before anything else happens -- then the value will not yet have been set.

Chris

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 18 replies
  • 3197 views
  • 6 likes
  • 2 in conversation