Help using Base SAS procedures

A short question

Reply
Frequent Contributor
Posts: 131

A short question

I have a data like the one below. For each person (with ID), he went to either cinema (1,2) at different time. Just ignore the variable date. I want to know how a program can be developed in the DATA step to resolve the following question:

Get a summary for each person (ID) in terms of their frequency of visit to the two cinemas, using only the DATA step?

I want to program to get the results

Cinema ID Date
1 1 11/02/2007
1 2 11/02/2007
1 2 15/04/2007
1 2 20/06/2007
1 3 14/11/2007
2 1 05/01/2007
2 1 12/05/2007
2 2 08/08/2007

The result should look like this:
Cinema ID Freq
1 1 1
1 2 3
1 3 1
2 1 2
2 2 1
SAS Super FREQ
Posts: 8,740

Re: A short question

Hi:
This task seems tailor-made for PROC FREQ. Is there a reason you want a DATA step when PROC FREQ will create an output data set with the exact information you need???

Just curious.

cynthia
Frequent Contributor
Posts: 131

Re: A short question

Hi, I got a dataset which requires the use of DATA step to handle it. Better to use DATA step rather than any procedure.
Super Contributor
Super Contributor
Posts: 3,174

Re: A short question

Again - why would you need/want to "require" a DATA step, if the task can be completed in fewer SAS programming statements and possibly more efficiently?

And, so, with a DATA step, you must first sort your data, and then use BY GROUP PROCESSING to identify FIRST. and LAST. conditions for your defined BY variable list and the .

Explore the available DOC -- see the GOOGLE search below.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

data step by group processing site:sas.com
Frequent Contributor
Posts: 131

Re: A short question

Thanks, I will try to get a solution using first. and last. Cheers.
Contributor
Posts: 23

Re: A short question

While I agree that proc freq would be the logical way to go, if you must use a data step this should get you started.

data cinema;
input cinema id date ddmmyy10.;
datalines;
1 1 11/02/2007
1 2 11/02/2007
1 2 15/04/2007
1 2 20/06/2007
1 3 14/11/2007
2 1 05/01/2007
2 1 12/05/2007
2 2 08/08/2007
;
run;

data cinema2 (drop=date total);
retain total;
set cinema;
by cinema id notsorted;

if first.id then total = 0;
total+1;
count = total;
if last.id;
run;

Rich
Valued Guide
Posts: 2,174

Re: A short question

Data ;
do freq = 1 by 1 until( last.id ) ;
set your_data ;
By cinema id ;
End;
Run;
Almost as short as PROC FREQ
Occasional Contributor
Posts: 13

Re: A short question

Oh dear, such a neat and clever approach.

Any resource to recommend?
Valued Guide
Posts: 2,174

Re: A short question

Kevin

you are the resource
(just like all other posters)

here is the resource
(just like a lot of other discussion forums)

how long the list of SAS-lists?
longer than we can imagine!

What it used to be, was bit list server (circa 1986 ++ )
later, sas-l on mirrored mail servers
and "use net"

later google bought comp,soft-sys.sas
. . . . or ... brought comp.soft-sys.sas into google groups
and a (loose) gateway between was maintained

but, I think, I heard it's no longer effective
(call it defective?)

and we fragment the discussions some more

volume on SAS-L used to be around 80 messages per day (I could almost cope)
(before support.sas.com/forums)
(before spam)

possible we now have 80+ discussion channels

fragmentation is not always bad

but does make it difficult - - - to
keep up
to date
Contributor
Posts: 66

Re: A short question

try this:

proc freq data=cinema;
tables cinema*id/norow nocol nopercent nocum out=b;
run;
proc print;
run;
Ask a Question
Discussion stats
  • 9 replies
  • 201 views
  • 0 likes
  • 7 in conversation