DATA Step, Macro, Functions and more

How to add a flag on multiple records?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

How to add a flag on multiple records?

Hi, I have 1000 records in my file and i need to print a flag or variable or word 'Ind' (basically an indicator for me) on 100th position in every record.How i can achieve this?

Thank you in advance!

Accepted Solutions
Solution
Friday
Occasional Contributor
Posts: 18

Re: How to add a flag on multiple records?

Achieved this by using FORMAT statement, able to print 'IND' on all records.

View solution in original post


All Replies
Super User
Super User
Posts: 9,813

Re: How to add a flag on multiple records?

Post test data in the form of a datastep, we cannot see your computer!

data want;
  set have;
  if mod(_n_,100)=0 then flag=1;
run;

Not tested.

Occasional Contributor
Posts: 18

Re: How to add a flag on multiple records?

Let me clear it out... I have an existing file like below

column--> Name Age Gender
rows-->. Nesco 43 M
Greg 36 M

I want to add an extra column named indicator so that my file should look like below
(Desired dataset)

Nesco 43 M IND
Greg 36 M IND

I want IND on fixed position for all the records.

I hope now you are aware of my requirements
Super User
Super User
Posts: 9,813

Re: How to add a flag on multiple records?

Please review the how to post a question guide below the Post button.  Provide test data in the form of a datastep and show what the output should look like.

Hence the response remains the same:

data want;
  set have;
  if mod(_n_,100)=0 then indicator="IND";
run;
Occasional Contributor
Posts: 18

Re: How to add a flag on multiple records?

Existing dataset:
Nesco 43 M
Greg 36 M

Desired: (to add IND for all records in last column)

Nesco 43 M IND
Greg 36 M IND
Super User
Super User
Posts: 9,813

Re: How to add a flag on multiple records?

I will try once more.  Please provide test data in the form of a datastep which accurately shows your problem.  What you have posted neither shows the data in a form that we can run, nor does it illustrate what to do or match the thread title.  Let me take my time to type some test data in for you and show you how to do it:

Here is some test data which we can run and have something to work with:

data have;
  input name $ age sex $;
datalines;
Nesco 43 M
Greg 36 M
Nesco 43 M
Greg 36 M
Nesco 43 M
Greg 36 M
Nesco 43 M
Greg 36 M
Nesco 43 M
Greg 36 M
;
run;

Your thread title was how to flag X number of rows each time, to which I presented the code:

data want;
  set have;
  if mod(_n_,3)=0 then indicator="IND";
run;

If you put these two together you will fin that every 3rd row is flagged.  If this is not what you want, please describe thoroughly, providing test data in the form of a datastep and showing what you want out at the end.

Super Contributor
Posts: 354

Re: How to add a flag on multiple records?

Hello,

 

I will take a wild guess and assume that you want to create a new column at a predefined position in the dataset.

 

Here is a program that adds a column in sashelp.class in third position :

%let pos=3;

proc sql;
	SELECT name
	INTO :before_pos separated by ','
	FROM dictionary.columns
	WHERE libname="SASHELP" AND memname="CLASS" AND varnum<&pos.;

	SELECT name
	INTO :after_pos separated by ','
	FROM dictionary.columns
	WHERE libname="SASHELP" AND memname="CLASS" AND varnum>=&pos.;

	CREATE TABLE WANT AS
	SELECT &before_pos., "IND" AS newvar, &after_pos.
	FROM sashelp.class;
quit;
Solution
Friday
Occasional Contributor
Posts: 18

Re: How to add a flag on multiple records?

Achieved this by using FORMAT statement, able to print 'IND' on all records.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 102 views
  • 1 like
  • 3 in conversation