BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bhavanaa44
Fluorite | Level 6
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
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

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?

 

Bhavanaa44
Fluorite | Level 6
I got this question in interview.
They have just given me the output and told me to write program for it.
novinosrin
Tourmaline | Level 20

@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

 

novinosrin
Tourmaline | Level 20

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

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Bhavanaa44
Fluorite | Level 6

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

 

 

Bhavanaa44
Fluorite | Level 6
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
Patrick
Opal | Level 21

@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 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1640 views
  • 1 like
  • 4 in conversation