DATA Step, Macro, Functions and more

How to use First. To get the following output.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

How to use First. To get the following output.

Obs. Number name score N
1. 1 David. 54. 1
2. 1. David. 75 2
3. 2. Sam. 24. 1
4. 2. Ram. 35. 1

Accepted Solutions
Solution
3 weeks ago
Trusted Advisor
Posts: 1,312

Re: How to use First. To get the following output.

Posted in reply to Bhavanaa44

It looks like you want the first two records for each by group with 2 or more records.  And the first record for any single-record by-group.  It also looks like you have to produce N=1, N=2.

 

data want;
  set have;
  by name;
  if first.name then N=1;
else N+1;   if N<=2;
run;

View solution in original post


All Replies
PROC Star
Posts: 1,605

Re: How to use First. To get the following output.

Posted in reply to Bhavanaa44

Hi, You wrote-"

How to use First. To get the following output."

 

1. The following output from where( which input data sample)?

2. What do you want to accomplish?

 

Occasional Contributor
Posts: 5

Re: How to use First. To get the following output.

Posted in reply to Bhavanaa44
I got this question in interview.
They have just given me the output and told me to write program for it.
PROC Star
Posts: 1,605

Re: How to use First. To get the following output.

Posted in reply to Bhavanaa44

@Bhavanaa44  Ok, I am assuming the interviewer perhaps wanted to test your knowledge on by group processing in a datastep and that's the same he/she has given you. Apparently, the interviewer wants you to apply first and last automatic variables on the sample and output the first. var for the records. 

I suppose the interviewer wants to pick the first score of each of the names that happens to the grouping variable.

 

 

Something like the following is what perhaps the interviewer was seeking

 

data want;

set have;

by name score;

if first.score;

run;
after sorting the input(have) by name and score

 

PROC Star
Posts: 1,605

Re: How to use First. To get the following output.

Posted in reply to novinosrin

If you are looking at DW/BI/Analytics careers using SAS, I suggest you to invest in some sas books by authors ron cody, art carpenter to speed up your learning

Solution
3 weeks ago
Trusted Advisor
Posts: 1,312

Re: How to use First. To get the following output.

Posted in reply to Bhavanaa44

It looks like you want the first two records for each by group with 2 or more records.  And the first record for any single-record by-group.  It also looks like you have to produce N=1, N=2.

 

data want;
  set have;
  by name;
  if first.name then N=1;
else N+1;   if N<=2;
run;
Occasional Contributor
Posts: 5

Re: How to use First. To get the following output.

Hi, 

 

i have run the code but output didn't come as desired. 

Question asked :

Obs. Number name score N
1. 1 David. 54. 1
2. 1. David. 75 2
3. 2. Sam. 24. 1
4. 2. Ram. 35. 1

 

 

Please find my code.

 

data x;
input number name $ score;
cards;
1 David 75
2 Sam 24
2 Ram 35
1 David 54
;
run;

proc sort data=x;
by name score;
run;

data want;
set x;
by name score;
if first.name then N=1; else N+1;
if N<=2;run;

 

 

Output after running above code:

Obs. Number name score N
1. 1 David   54  1
2. 1. David  75 2
3. 2. Ram   35 1
4. 2. Sam   24 1

 

 

Occasional Contributor
Posts: 5

Re: How to use First. To get the following output.

Posted in reply to Bhavanaa44
I will run this code and see if the same output is coming or not.

One more thing is there any way to install SAS in windows 10. As I have in VMware and it is very slow
Respected Advisor
Posts: 4,698

Re: How to use First. To get the following output.

[ Edited ]
Posted in reply to Bhavanaa44

@Bhavanaa44 wrote:
I will run this code and see if the same output is coming or not.

One more thing is there any way to install SAS in windows 10. As I have in VMware and it is very slow

 

Yes, that should work. 

The best place to get answers to such questions is the SAS Install Center. 

https://support.sas.com/documentation/installcenter/

https://support.sas.com/en/documentation/system-requirements.html

http://support.sas.com/documentation/installcenter/en/ikfdtnwinsr/67228/PDF/default/sreq.pdf 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 221 views
  • 1 like
  • 4 in conversation