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

Hi, 

I'm a beginner SAS user. I have a dataset with N number of rows/observations. Then I have a separate, dataset with a column called Rank, this second dataset is bigger than the first one.

What I want to do, is I want to cut out all rows from my second dataset with a rank higher than N. 

I have this now

 

DATA Want1 Want2;
SET Have;
IF (Rank > N) THEN OUTPUT Want1;
IF (Rank <= N) THEN OUTPUT Want2;
RUN;

 

But I don't know how to make N. How do get the Number of Rows (or number of URNs, if it's easier with a defined column name) to use here?

Thanks, 
Lotte

ps I'm using SAS Enterprise Guide 7.13

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, one way:

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="HAVE"));
  call symput('N',nobs);
run;

data want1 want2;
  set have;
  if ... > &n. then output want1;
  else output want2;
run;

Really not sure about your if logic of rank > N?  Maybe provide some test data and required output to clarify.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, one way:

data _null_;
  set sashelp.vtable (where=(libname="WORK" and memname="HAVE"));
  call symput('N',nobs);
run;

data want1 want2;
  set have;
  if ... > &n. then output want1;
  else output want2;
run;

Really not sure about your if logic of rank > N?  Maybe provide some test data and required output to clarify.

Lottek
Calcite | Level 5

Thank you for your answer!

This worked when my dataset with N observations had observations in it. When it's empty, my variable N is empty and it doesn't run. Is there a way to tell it that if N is null, put 0?

Thanks, 

Lotte

Kurt_Bremser
Super User

The placement of statements in the data step is crucial. Note where @RW9 put his call symput, and where I put mine.

Slightly expanded version of my code to show the effect:

data class;
set sashelp.class (obs=0);
run;

data _null_;
call symput('n',put(number,best.));
put "in one iteration";
set work.class nobs=number;
stop;
run;

%put n=&n;

Log from this:

24         data class;
25         set sashelp.class (obs=0);
26         run;

NOTE: There were 0 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 0 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds
      

27         
28         data _null_;
29         call symput('n',put(number,best.));
30         put "in one iteration";
31         set work.class nobs=number;
32         stop;
33         run;

in one iteration
NOTE: There were 0 observations read from the data set WORK.CLASS.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

34         
35         %put n=&n;
n=           0

Note that this was run on a pristine SAS session to make sure that &n. was not left from a previous run.

Now I move the set statement to the top in my data _null_, and this is the log:

24         data class;
25         set sashelp.class (obs=0);
26         run;

NOTE: There were 0 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.CLASS has 0 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

27         
28         data _null_;
29         set work.class nobs=number;
30         call symput('n',put(number,best.));
31         put "in one iteration";
32         stop;
33         run;

NOTE: There were 0 observations read from the data set WORK.CLASS.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

34         
35         %put n=&n;
WARNING: Apparent symbolic reference N not resolved.
n=&n

(once again on a fresh SAS session)

The call symput (and the put statement I inserted for debugging) is never reached, as the EOF that happens at the set statement immediately terminates the data step.

If you are puzzled why the nobs= variable is available and set before the set statement: This variable is initialized with the nobs value before the first iteration even starts; you could see this as a declarative characteristic of the set statement.

Kurt_Bremser
Super User

Look at sashelp.vtable (or dictionary.tables in PROC SQL). The number of observations is found in column nobs.

proc sql;
select nobs into :n from dictionary.tables where libname = 'YOURLIB' and memname = 'YOURDATASET';
quit;

Another way is this:

data _null_;
call symput('n',put(number,best.));
set yourlib.yourdataset nobs=number;
stop;
run;

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