BookmarkSubscribeRSS Feed
KrunalPatel
Calcite | Level 5

Hi,

anybody can help me how can i initiate my _n_ from 1 when my condition is met.

I am using following code to do that. It is working but it has problem where _n_ is linked to 'IF' condition.

i.e. if _Type_ = 2 is appearing in row number 5 in my table "AirCont" then my call symputx outcome gives me the outome to 'Y5' , whereas I want that _N_ initiate/incriment only when condition is met.

data _null_;

set AirCont;

if _Type_ = 2 then call symputx(compress('Y' || _n_),year);

run;

Thanks,

KP

3 REPLIES 3
Reeza
Super User

Don't use the automatic variable _n_  . Create your own counter and use that instead.

Tom
Super User Tom
Super User

Perhaps I don't understand the question?

If you want your own counter just make your own counter.

data _null_;

  set AirCont;

  if _type_=2 then do;

    n+1;

    call symputx(compress('Y' || n),year);

  end;

run;

If you want to use the _N_ as the count of how many times the data step as iterated then change your IF to a WHERE.

data _null_;

  set AirCont;

  where _type_=2 ;

  call symputx(compress('Y' ||_n_),year);

run;

KrunalPatel
Calcite | Level 5

Hi Reeza - sorry I was not clear enough.

Tom - Thanks i havent tried yet but your reply will work.

Thanks All

KP

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
  • 2038 views
  • 1 like
  • 3 in conversation