4月 222016
 

ProblemSolversThe DS2 programming language gives you the following powerful capabilities:

  • The precision that results from using the new supported data types
  • Access to the new expressions, write methods, and packages available in the DS2 syntax
  • Ability to execute SAS Federated Query Language (FedSQL) from within the DS2 program
  • Ability to execute code outside of a SAS session such as on SAS® High-Performance Analytics Server or the SAS® Federation Server
  • Access to the threaded processing in products such as the SAS® In-Database Code Accelerator, SAS High-Performance Analytics Server, and SAS® Enterprise Miner™

Some DATA step functionality is not available in DS2, at least not how you are used to. However, don’t lose hope, because this article discusses ways to mimic some of the missing DATA step features within DS2.

Simulate Missing SET Statement Data Set Options

Many of the SET statement options are not allowed within DS2, such as the OBS= data set option. However, you can simulate some of these options by using FedSQL. For example, you can use a LIMIT clause similar to the following in the place of OBS=:

{select * from work.temp limit 10}.

Here is an example:

data one;                                                                                                                               
   do i = 1 to 100;                                                                                                                       
      output;                                                                                                                               
   end;                                                                                                                                   
run;                                                                                                                                    
                                                                                                                                        
proc ds2;                                                                                                                               
data new(overwrite=yes);                                                                                                               
   method run();                                                                                                                         
      set {select * from work.one limit 10};                                                                                               
   end;                                                                                                                                  
enddata;                                                                                                                               
run;                                                                                                                                    
quit;                                                                                                                                   
                                                                                                                                        
proc print data=new;                                                                                                                    
run;    

 

Prevent Errors from Duplicate Data Set Names

In a DATA step, an automatic overwrite occurs when you issue a DATA statement that contains a data set name that was used previously in your SAS session.

For example, in the DATA step, you can add the following code:

data one;
   x=100;
run;
data one;
   x=200;
run;

This code overwrites the previous ONE data set. However, this automatic overwrite does not occur within DS2, and an error is generated if a specified data set name already exists. To work around this problem, you can use the OVERWRITE option as shown below.
proc ds2;
data one(overwrite=yes);
   dcl double x;
   method init();
      x=100;
   end;
run;
quit;

Specify Name Literals

In a DATA step, you can use name literals. However, in DS2, they are specified differently.

In Base SAS® with the VALIDVARNAME system option set to ANY, you can use a name literal like the following:

'My var'n=100;

This strategy does not work in DS2, but you can use double quotation marks to get the same results:

"My var"=100;

Substitute Missing Statements

The ATTRIB, LABEL, LENGTH, FORMAT, and INFORMAT statements are missing from DS2. However, you can use the DECLARE statement with the HAVING clause to perform these functions.

Here is an example:

dcl double aa having
   label 'var aa'
   format comma8.2;

Create an Array

In Base SAS, you use the ARRAY statement in a DATA step to create an array to reference variables within the program data vector (PDV). Here is an example:

data one;                                                                                                                               
   array list(4) x1-x4;                                                                                                                  
   do i = 1 to 4;                                                                                                               
      list(i)=i;                                                                                                                          
   end;                                                                                                                                 
run;

The ARRAY statement does not exist in DS2, but the following code shows how to use an equivalent statement called VARARRAY:
proc ds2;                                                                                                                               
data one(overwrite=yes);                                                                                                               
   vararray double x[4];                                                                                                                 
   declare double i;                                                                                                                     
   method init();                                                                                                                       
   do i = 1 to 4;                                                             
      x[i]=i;                                                                                                                            
   end;                                                                                                                                
   end;                                                                                                                                 
enddata;                                                                                                                               
run;                                                                                                                                    
quit;  

Note: The VARARRAY statement must be outside the METHOD statement.

Enable Macro Variable Resolution

To reference a macro variable as a character value in the DATA step, you place double quotation marks around the macro variable as shown in the following example:

%let val=This is a test;
data _null_;
   dval=”&val”;
   put dval=;
run;

In DS2, double quotation marks are used only to delimit an identifier. Single quotation marks are required to delimit constants. If the above code was run within DS2, a warning similar to the following would occur:

Solutions for missing DATA step features within DS2

To get a better understanding of the difference between an identifier and constant text, consider the following two examples:

VARA=’test’;
VARB=”vara”;

Within DS2, the first assignment statement creates a variable called VARA and assigns it a text string of test. The second assignment statement creates a variable called VARB and also assigns it a text string of test. Since the second assignment statement is using double quotation marks, vara is seen as an identifier and the identifier’s value is placed into the variable VARB.

Since constant text is represented by single quotation marks in DS2, there needs to be a way to resolve the macro variable within quotation marks. Luckily, within DS2, there is a SAS supplied autocall macro called %TSLIT that enables macro variable resolution within single quotation marks.  Here is an example:

%let val=This is a test;                                                                                                                
proc ds2;                                                                                                                               
data _null_;                                                                                                                           
   method init();                                                                                                                         
      declare char(14) dval;                                                                                                               
      dval=%tslit(&val);                                                                                                                  
      put dval=;                                                                                                                          
   end;                                                                                                                                   
enddata;                                                                                                                               
run;                                                                                                                                    
quit;

I hope this blog post has been helpful. If you have any questions, please contact SAS Technical Support and we will be happy to assist you. Thanks for using SAS!

 

tags: Problem Solvers, PROC DS2, SAS Programmers

Solutions for missing DATA step features within DS2 was published on SAS Users.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)