DATA Step, Macro, Functions and more

Adding a custom OBS variable

Reply
N/A
Posts: 0

Adding a custom OBS variable

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.
Contributor
Posts: 74

Re: Adding a custom OBS variable

n=_n_+100;

does this work?
SAS Super FREQ
Posts: 8,743

Re: Adding a custom OBS variable

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
N/A
Posts: 0

Re: Adding a custom OBS variable

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.
N/A
Posts: 0

Re: Adding a custom OBS variable

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.
Super Contributor
Super Contributor
Posts: 3,174

Re: Adding a custom OBS variable

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.
Ask a Question
Discussion stats
  • 5 replies
  • 164 views
  • 0 likes
  • 4 in conversation