BookmarkSubscribeRSS Feed
JMES
Calcite | Level 5

Hello,

 

I have been trying to figure out what I am doing wrong, but can't, so here I am posting the problem hoping someone can provide me some solution.

 

The Problem:

I have a IF statement within Do loop within DATA step. The IF statement evaluates by comparing Do loop index with a macro variable. Somehow the two values in comparison are not properly evaluated and thus, the codes always return ELSE values.

 

%macro TEMP();

%do i = 1 %to &Sample.;
    %do j = 1 %to &DD.;

         /*some codes*/    

        %let dsid = %sysfunc(open(TEMP_0));
        %let CNT_0 = %sysfunc(attrn(&dsid,NOBS));
        %let CC = %sysfunc(close(&dsid));
%let dsid = %sysfunc(open(TEMP_00)); %let CNT_00 = %sysfunc(attrn(&dsid,NOBS)); %let CC = %sysfunc(close(&dsid)); /*some codes*/ data TEMP_0_; set TEMP_0_; data TEMP_0_; set TEMP_0_; do k = 1 to &CNT_0.; if k <= &CNT_00. then Rand = 1; /*k and CNT_oo comparison is not properly evaluated*/ else Rand = 0; /* thus Rand is always 0*/ end; drop k; run; /*some codes*/ %mend TEMP;

 

3 REPLIES 3
FreelanceReinh
Jade | Level 19

Hello @JMES,

 

Your code is syntactically correct (assuming that the %ENDs for the two %DO loops are hidden in "some codes").

 

I cannot replicate your issue. When I tested it with appropriate datasets TEMP_0 and TEMP_00, variable RAND was set to 1.

 

The duplicate line "data TEMP_0_; set TEMP_0_;" is redundant. The second data step could be simplified as follows:

data TEMP_0_;
set TEMP_0_;
Rand=ifn(&CNT_0, &CNT_0 <= &CNT_00, .);
run;

 

Could you please provide small sample datasets (in the form of a data step) which demonstrate the issue?

 

 

 

Astounding
PROC Star

I suppose this might happen if your second data set contains 0 observations.  But you can make it easy to diagnose.  Just add this before you run:

 

options symbolgen;

 

Then you will be able to see the values you are working with for your macro variables.

JMES
Calcite | Level 5

Thanks all.

 

I realized that the codes that I wrote are not doing what I wanted it to do.

I chaged the codes and working fine now.

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!

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