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;
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 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
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
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;
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 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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.