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-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
  • 5 replies
  • 1011 views
  • 0 likes
  • 4 in conversation