SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2661 views
  • 2 likes
  • 4 in conversation