BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASInd
Obsidian | Level 7
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!
1 ACCEPTED SOLUTION

Accepted Solutions
SASInd
Obsidian | Level 7

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

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

SASInd
Obsidian | Level 7
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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
SASInd
Obsidian | Level 7
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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

gamotte
Rhodochrosite | Level 12

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;
SASInd
Obsidian | Level 7

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

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