DATA Step, Macro, Functions and more

SAS program to read the nth observation from a dataset by file statement

Reply
Contributor rk7
Contributor
Posts: 28

SAS program to read the nth observation from a dataset by file statement

can anyone help me in writing the following program

 

* Using correct input option create a SAS data set named icecream using data file (Sales.dat);
* Use variables: Flavor, Location, BoxesSold
* Output only 5 observations starting with third observation.

Super User
Posts: 2,050

Re: SAS program to read the nth observation from a dataset by file statement

[ Edited ]

 

 

data icecream;

set sales(firstobs=3 obs=7);

keep flavor location boxessold;

run;

or something like

 

DATA icecream;
INFILE 'c:\MyRawData\sales.dat';
INPUT (flavor location) ($) boxessold firstobs=3 obs=7;
RUN;

 

 

 

Contributor rk7
Contributor
Posts: 28

Re: SAS program to read the nth observation from a dataset by file statement

Posted in reply to novinosrin

This worked

 

title 'dataset ICECREAM';
data iceceam;
infile '/folders/myfolders/data/Sales1.dat' firstobs=3 obs=7 ;
input flavor $11. location boxessold ;
run;

proc print ;
run;

 

Thanks for the effort.

Super User
Posts: 23,998

Re: SAS program to read the nth observation from a dataset by file statement

@rk7 That was @novinosrin answer to you yesterday. Marking your own answer as correct isn't appropriate in this situation.

 

Contributor rk7
Contributor
Posts: 28

Re: SAS program to read the nth observation from a dataset by file statement

There is a mistake in the solution he offered. The firstobs and obs dataset options are declared in input statement where as it should be in infile statement. I have no problem in removing the solution tag if @novinosrin changes his answer.

Thanks

 

Super User
Posts: 23,998

Re: SAS program to read the nth observation from a dataset by file statement

Nope, fair enough. Thanks for clarifying Smiley Happy

Super User
Posts: 2,050

Re: SAS program to read the nth observation from a dataset by file statement

Hahaha, No worries. I'm here to learn like you are. Also being a graduate student, community credit is pale in comparison to how much i feel inferior when i notice people like reeza, art t, xia keshan , haikuo, chris nz, mkeintz, astounding etc with their slick answers. The next semester is what I am hoping to gain in a big way to be able to solve harder questions when I move on to take courses in Regression, principal components,  time series and forecasting etc. Join the club if you like Smiley Happy . We are peers unlike the super list I mentioned above

Contributor rk7
Contributor
Posts: 28

Re: SAS program to read the nth observation from a dataset by file statement

Posted in reply to novinosrin

what a humble answer. loved it. It will remain an unanswered question

Contributor rk7
Contributor
Posts: 28

Re: SAS program to read the nth observation from a dataset by file statement

It asks me to accept one of the following as answer. so I select the answer
Super User
Posts: 23,998

Re: SAS program to read the nth observation from a dataset by file statement

Two ways:

 

FirstOBS and OBS via a data step

 

Or read it all in and filter afterwards using IF statements and _N_.

 


rk7 wrote:

can anyone help me in writing the following program

 

* Using correct input option create a SAS data set named icecream using data file (Sales.dat);
* Use variables: Flavor, Location, BoxesSold
* Output only 5 observations starting with third observation.


 

Contributor rk7
Contributor
Posts: 28

Re: SAS program to read the nth observation from a dataset by file statement

This is the code I have written 

 

data icecream;
INFILE '/folders/myfolders/data/Sales1.dat';
INPUT flavor $10. location boxessold;
if _N_ = 3 to 8 then output;
end;
RUN;

proc print;
run;

 

This is the output I got

 

 Obs flavor location 

Chocolate213123
Chocolate213123
Chocolate213123
Chocolate213123
Chocolate213123
Chocolate213123
Vanilla213512
Vanilla213512
Vanilla213512
Vanilla213512
Vanilla213512
Vanilla213512
Pecan415242
Pecan415242
Pecan415242
Pecan415242
Pecan415242
Pecan415242
Choco 213123
Choco 213123
Choco 213123
Choco 213123
Choco 213123
Choco 213123
Strawberry213512
Strawberry213512
Strawberry213512
Strawberry213512
Strawberry213512
Strawberry213512
Coffee415242
Coffee415242
Coffee415242
Coffee415242
Coffee415242
Coffee415242
nuts213123
nuts213123
nuts213123
nuts213123
nuts213123
nuts213123
almonds213512
almonds213512
almonds213512
almonds213512
almonds213512
almonds213512
mint415242
mint415242
mint415242
mint415242
mint415242
mint415242
Fudge213123
Fudge213123
Fudge213123
Fudge213123
Fudge213123
Fudge213123
Peanut213512
Peanut213512
Peanut213512
Peanut213512
Peanut213512
Peanut213512
Pistachio415242
Pistachio415242
Pistachio415242
Pistachio415242
Pistachio415242
Pistachio415242
Cherries213123
Cherries213123
Cherries213123
Cherries213123
Cherries213123
Cherries213123
Berry213512
Berry213512
Berry213512
Berry213512
Berry213512
Berry213512
Toffee415242
Toffee415242
Toffee415242
Toffee415242
Toffee415242
Toffee415242

 

This time I attach the file sales1.doc

Super User
Posts: 23,998

Re: SAS program to read the nth observation from a dataset by file statement

Look at your log and post your log.

Also, you posted a bunch of information but didn't clarify what you're asking in this post, since your original question has answers.

 


rk7 wrote:

This is the code I have written 

 

data icecream;
INFILE '/folders/myfolders/data/Sales1.dat';
INPUT flavor $10. location boxessold;
if _N_ = 3 to 8 then output;
end;
RUN;

proc print;
run;

 

This is the output I got

 

 Obs flavor location 

Chocolate 213 123
Chocolate 213 123
Chocolate 213 123
Chocolate 213 123
Chocolate 213 123
Chocolate 213 123
Vanilla 213 512
Vanilla 213 512
Vanilla 213 512
Vanilla 213 512
Vanilla 213 512
Vanilla 213 512
Pecan 415 242
Pecan 415 242
Pecan 415 242
Pecan 415 242
Pecan 415 242
Pecan 415 242
Choco 2 13 123
Choco 2 13 123
Choco 2 13 123
Choco 2 13 123
Choco 2 13 123
Choco 2 13 123
Strawberry 213 512
Strawberry 213 512
Strawberry 213 512
Strawberry 213 512
Strawberry 213 512
Strawberry 213 512
Coffee 415 242
Coffee 415 242
Coffee 415 242
Coffee 415 242
Coffee 415 242
Coffee 415 242
nuts 213 123
nuts 213 123
nuts 213 123
nuts 213 123
nuts 213 123
nuts 213 123
almonds 213 512
almonds 213 512
almonds 213 512
almonds 213 512
almonds 213 512
almonds 213 512
mint 415 242
mint 415 242
mint 415 242
mint 415 242
mint 415 242
mint 415 242
Fudge 213 123
Fudge 213 123
Fudge 213 123
Fudge 213 123
Fudge 213 123
Fudge 213 123
Peanut 213 512
Peanut 213 512
Peanut 213 512
Peanut 213 512
Peanut 213 512
Peanut 213 512
Pistachio 415 242
Pistachio 415 242
Pistachio 415 242
Pistachio 415 242
Pistachio 415 242
Pistachio 415 242
Cherries 213 123
Cherries 213 123
Cherries 213 123
Cherries 213 123
Cherries 213 123
Cherries 213 123
Berry 213 512
Berry 213 512
Berry 213 512
Berry 213 512
Berry 213 512
Berry 213 512
Toffee 415 242
Toffee 415 242
Toffee 415 242
Toffee 415 242
Toffee 415 242
Toffee 415 242

 

This time I attach the file sales1.doc



 

 

Ask a Question
Discussion stats
  • 11 replies
  • 481 views
  • 5 likes
  • 3 in conversation