BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
noobs
Calcite | Level 5

Hello EG Users,

Is it possible to read length of dataset? I am interested in counting number of observations in a dataset and save it as macro variable, so that I can access it later in another data step.

data have;

input NumProduct 2.;

datalines;

11

21

31

41

51

;

run;

%Let havelength = 5;

data have1;

set have;

do i=1 to &havelength;

MultipleOfFive = NumProduct * 5;

output;

end;

run;

Expecting have1 to have following observations:

55

105

155

205

255

Any suggestions on how to calculate 5 in %Let macro statement so that even if number of observations change dynamically in dataset have, dataset have1 will get populated correctly?

thanks,

Dhanashree

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

For your example program you don't need the value in a macro variable at all.  You can also get it in any data step with something like this again not need for macro variable.

if 0 then set have(drop=_all_) nobs=nobs;

data have;
   input NumProduct :2. @@;
   datalines;
11 21 31 41 51
;;;;
   run;


data have1;
   set have nobs=havelength;
   do i=1 to havelength;
      MultipleOfFive = NumProduct * 5;
     
output;
     
end;
  
run;

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

There are a number of ways to do that.  e.g.:

data _null_;

  if 0 then set have nobs=nobs;

  call symput('havelength',nobs);

  stop;

run;

data_null__
Jade | Level 19

For your example program you don't need the value in a macro variable at all.  You can also get it in any data step with something like this again not need for macro variable.

if 0 then set have(drop=_all_) nobs=nobs;

data have;
   input NumProduct :2. @@;
   datalines;
11 21 31 41 51
;;;;
   run;


data have1;
   set have nobs=havelength;
   do i=1 to havelength;
      MultipleOfFive = NumProduct * 5;
     
output;
     
end;
  
run;
noobs
Calcite | Level 5

That works gret.. really appreciate shedding light on use of nobs!

Thanks much Smiley Happy

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1005 views
  • 0 likes
  • 3 in conversation