BookmarkSubscribeRSS Feed
GN0001
Barite | Level 11

Hello team,

 

I have inherited a code that I run on a monthly basis.

 

data mydata;

set mydata1 where (keep this that datatime where=(datepart(datetime) >= "01012021"

mydata2 (keep=datetime fileNum);

cnt = 1;

date1 = datepart(datetime);

datemount = put(date1, yymmn6.);

if datemount > "202201" then output;

run;

 

proc summary nway missing data =mydata;

var cnt;

........

run;

on the first statement? Does cnt becomes 1 when the first time SAS reads the first record and then it becomes 2 on the second row since SAS reads sequentially. 

 

What is the value of cnt on the second statement? Is it incremented?

 

Can anyone please kindly explain it to me?

 

Regards,

BlueBerry

 

 

 

Blue Blue
3 REPLIES 3
Tom
Super User Tom
Super User

What is the value of cnt in the statement?

Since the code sets CNT to one on every observations that means the variable cnt will have one on every observation.

 

But another way to interpret that questions is 

What value does the CNT variable add to this code 

It kind of depends on what you are using PROC SUMMARY to do in that last step.  But it does not look like it is adding any value.  Let's do a simple test using SASHELP.CLASS.

proc summary data=sashelp.class;
 output out=want;
run;

proc print data=want;
run;

data class;
 set sashelp.class;
 cnt=1;
run;

proc summary data=class;
  var cnt;
  output out=want;
run;

proc print data=want;
run;

The only useful statistic is the count of observations that the _FREQ_ variable already provided.

Tom_0-1660270910566.png

 

 

 

pink_poodle
Barite | Level 11
It seems that variable cnt is a vestige. It is a counter that was not fully enacted. To make it work, for example, by counting the number of outputted observations with datemount > “202221”, you would need to put it in a do-loop like instead of “then output” write
…then do;
Output;
Cnt + 1;
End;
The top condition would make more sense with cnt = 0, or the counter will show one more observation.
Kurt_Bremser
Super User

Your code won't run anyway because of syntax errors:

set mydata1 where (keep this that datatime where=(datepart(datetime) >= "01012021"
mydata2 (keep=datetime fileNum);

You have four opening, but only two closing brackets.

Do you really want to include a dataset named "WHERE"?

The result of DATEPART is a number, so comparing it to a string won't work.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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