BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello everyone,

I have this simply code:
[pre]
proc sort data=SRC; by Region descending GDP; run;

DATA MAKETHIS;
length Region 3 Country $30 GDP 5;
set SRC;
keep Region Country GDP;
by Region;
if first.Region then output;
RUN;[/pre]

... which takes the Country with the highest GDP for each Region (so is the purpose of the PROC SORT). This code fully works with no problems but I want to add a column/variable (specifically in first place) called n. This variable is meant to be the observation number +100.

And so I have 2 problems:

1) Is there any instrustion in SAS that will allow me to get the row/obs number? Because then I could use that and add 100 to get what I need.

2) I don't know how to change the code without creating another data step. If I do this:
[pre]
DATA MAKETHIS;
length n 3 Region 3 Country $30 GDP 5;
set SRC;
n=100;
keep Region Country GDP;
by Region;
if first.Region then output;
RUN;
[/pre]
This doesn't work, this variable n doesn't appear. But if I add this data step..:
[pre]
DATA MAKETHIS;
length n 3 Region 3 Country $30 GDP 5;
set MAKETHIS;
n = 100;
RUN;
[/pre]
...no problem. Ofcourse this doesn't seem like the best of all options.

Any suggestion?
Thank-you in advance.
5 REPLIES 5
abdullala
Calcite | Level 5
n=_n_+100;

does this work?
Cynthia_sas
SAS Super FREQ
Hi:
Carefully examine your KEEP statement. If you want the variable N to be included in the output dataset WORK.MAKETHIS, then ALL the variables you want to KEEP in WORK.MAKETHIS must be listed in your KEEP statement.

Also, investigate in the documentation the automatic variable _N_, which counts the number of times a DATA step has executed or has looped. If the DATA step loops one time for every observation being read, then _N_ can sometimes be used as an observation counter -- for simple programs.

However, what if you have more than 100 observations in your INPUT file??? If the _N_ value is 523, then your calculated variable N will be 623. Is this what you really want??? Or do you know that your data will always have less than 100 obs?

cynthia
deleted_user
Not applicable
oooh! so that's what _N_ is for? I had seen that before but I never quite understood what it was. Thank-you abdullala, that works. But I still can't get it into 1 data step. This is what I have so far:
[pre]
proc sort data=PROJET.TEMP; by Region descending GDP; run;
DATA MAKETHIS;
length Region 3 Country $30 GDP 5;
set SRC;
keep Region Country GDP;
by Region;
if first.Region then output;
RUN;
DATA MAKETHIS;
length n 3 Region 3 Country $30 GDP 5;
set SRC;
n = 100 + _n_;
RUN;
[/pre]
How can I rewrite this into 1 data step? If I put n=_N_+100 anywhere after the length statement it doesn't work.
deleted_user
Not applicable
Never-mind my post. I got it, I was forgetting to add n to the KEEP statement as well as the LENGTH statement... silly me.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Please help by clarifying what is meant with "...anywhere after the length statement it doesn't work."

If you want the _N_ value of the first input file (+100), then the suggested assignment statement will work, as explained. However, if you are only counting the intermediate file's "output observations", then you will need to increment a count variable "_OUTOBS_+1" or something within a DO/END code paragraph, before your OUTPUT, and add add +100 to that variable to create your "N" value.

Scott Barry
SBBWorks, Inc.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1394 views
  • 0 likes
  • 4 in conversation