DS2 retain statement

Reply
Contributor
Posts: 21

DS2 retain statement

I am using the code below as example.  When I add BY statement ( by sex in my example), I get an error saying "

ERROR: Compilation error.

ERROR: Parse encountered BY when expecting end of input.

ERROR: Parse failed on line 122:  >>> by <<<  sex;."

What does this mean? Where can I add the BY statement?

Thank you,

Kayo

proc copy in = sashelp out = work;

    select class;

run;

proc ds2;

data;

    dcl double min;

   by sex;

    retain min;

    keep min;

    method init();

             min = 9999;

    end;

    method run();

             set work.class;

             if weight < min then min = weight;

    end;

    method term();

          output;

    end;

enddata;

run;

quit;

Valued Guide
Posts: 3,206

Re: DS2 retain statement

IT means: you cannot use a by statement at that location. Remember DS2 can run multithreaded, it is not the same as old datastep programming.

SAS(R) 9.4 DS2 Language Reference, Third Edition (ds2 language by statement) and SAS(R) 9.4 DS2 Language Reference, Third Edition (example 3)

---->-- ja karman --<-----
Frequent Contributor
Posts: 137

Re: DS2 retain statement

Is DS2 language aimed for advance SAS expert programmers like You, friedegg and haikuo et al or new starters lik me who can begin as DS2 language programmers? I read the functionality of intersecting sql and datastep, but from learning point of view, I'd like to know whom it is suited for?

Trusted Advisor
Posts: 1,300

Re: DS2 retain statement

, there is no reason to avoid DS2 or view it as something directed towards 'experts.'  While the learning curve may be view-able as steeper (which I would say depends on your background), it is a useful tool for any programmer.  I would recommend the following paper as a nice introduction to the procedure:

http://support.sas.com/resources/papers/proceedings14/1283-2014.pdf

Frequent Contributor
Posts: 137

Re: DS2 retain statement

Thank you Sir for your encouraging words. And you are too right!, the learning is indeed steep for the simple reason my grasping power of the vast concepts is much slower than the normal ones and comparing to you(expert) folks, my brain functions perhaps at 1/10th of your speed.  Nevertheless, I am determined not to give up despite how hard it presents.

I am just in awe of the fact to notice how quickly you guys grasp, understand, remember and apply, while SAS being extremely vast.  as it is or is it that you worked so hard losing all your sleep when you began learning this language. Anyways, Many thanks Once again Sir and happy new year.

Cheers,

Charlotte from England!

Trusted Advisor
Posts: 1,300

Re: DS2 retain statement

As said, mostly it's just having experience and I'll add: reading, a lot...

Contributor
Posts: 21

Re: DS2 retain statement

Thank you!

Trusted Advisor
Posts: 1,300

Re: DS2 retain statement

A BY statement must be preceded by statement to which it is meant to apply to.

51   proc ds2;

52   data;

53   dcl double min;

54   method run();

55   min=9999;

56   do until(last.sex);

57   set class;

58   by sex;

59   min = min(weight, min);

60   end;

61   put sex= min=;

62   end;

63   enddata;

64   run;

sex=F min=50.5

sex=M min=83

Trusted Advisor
Posts: 1,300

Re: DS2 retain statement

Or, a even better way, for DS2 would be to just use a SET statment with a SQL query

66   proc ds2;

67   data;

68   method run();

69   set {select sex, min(weight) as min from class group by sex};

70   put sex= min=;

71   end;

72   enddata;

73   run;

SEX=F MIN=50.5

SEX=M MIN=83

74   quit;

Respected Advisor
Posts: 3,124

Re: DS2 retain statement

Nice. I can see the prime time of DS2 is coming. This vaguely reminds me of the Partitioning function in PL/SQL. 

Contributor
Posts: 21

Re: DS2 retain statement

I have another question.  How can I save the output?  I am not sure where I add the code.

Thanks,

Kayo

Trusted Advisor
Posts: 1,300

Re: DS2 retain statement

Instead of a null data statement put you desired output name, just like the regular data step.

proc ds2;

data want;

method run();

set {select sex, min(weight) as min from class group by sex};

put sex= min=;

end;

enddata;

run;

quit;

Contributor
Posts: 21

Re: DS2 retain statement

Thanks!  It works!

Valued Guide
Posts: 3,206

Re: DS2 retain statement

Charlotte, all depends on your background, the technical environment and your personal goals.

You are seeing init run sections, those are part of SAS/AF scl language in a similar way.  The DS2 language is different to a dataset: SAS(R) 9.4 DS2 Language Reference, Third Edition (getting started).

When you:

- are mainly connecting to an external DBMS

- trying to use the functionality of that external DBMS as much as possible.

- Understand threading parallel processing and more strict type processing (object oriented)

Than you are a programmer that could well benefit from DS2

When you:

- are still using the way of thinking by using Excel

- Wanting all data being processed as SAS datasets and using SAS procs

- May be using only a click/drag programming interface

Than you are a programmer that could face a lot of problems learning - using DS2  

---->-- ja karman --<-----
Valued Guide
Posts: 3,206

Re: DS2 retain statement

Charlotte, it are not your brains that are of different speed. It is the way of recognizing similar patterns.

A lot in ICT - Analytics - organization processes - coming back over and over again with different buzz-words or acronyms. A would wish those patterns would be documented more clearly.

Once getting used to that and seeing those patterns makes it all more easy.

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 14 replies
  • 821 views
  • 5 likes
  • 5 in conversation