BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
littlestone
Fluorite | Level 6

Hello,

Suppose I have following dataset (" test"):

idvar_1
var_2
111
121
130
140
151
261
270
280
291
2101

I run follwoing codes:

proc sort data=test;

by id var_1 var_2;

run;

data test;

set test;

by id var_1 var_2;

if last.var_2 then var_3=1;

else var_3=0;

run;

What I expect is:

idvar_1var_2var_3
1110
1211
1300
1401
1510
2611
2700
2801
2910
21011

However, what I get is:

idvar_1var_2var_3
1111
1211
1301
1401
1511
2611
2701
2801
2911
21011

What I did is wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To get your requested output use BY VAR_2 NOTSORTED.

Not sure that actually makes any sense as it is crossing boundry between values of ID.

159  data want ;

160    set have ;

161    by var_2 notsorted;

162    var_3=last.var_2;

163    put (id var_1 var_2 var_3) (3.);

164  run;

  1  1  1  0

  1  2  1  1

  1  3  0  0

  1  4  0  1

  1  5  1  0

  2  6  1  1

  2  7  0  0

  2  8  0  1

  2  9  1  0

  2 10  1  1

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

The problem is the VAR_1 is different on every observation. So within the set of constant values for ID and VAR_1 every value of VAR_2 is unique. 

data want ;

   set test;

   by id var_2 notsorted;

   var_3 = last.var_2;

run;

art297
Opal | Level 21

I think you are trying to do something like:

data test;

  set test;

  by id var_2 notsorted;

  if last.var_2 then var_3=1;

  else var_3=0;

run;

However, that would meet all of your expectations except for the 5th record which, if I correctly understand what you are trying to do, was incorrect in your example.

littlestone
Fluorite | Level 6

Thank you all very much for help. I never know there is a "notsorted" option.

By the way, could you please elaborate why 5th record is incorrect? I couldn't figure it out myself...

Tom
Super User Tom
Super User

To get your requested output use BY VAR_2 NOTSORTED.

Not sure that actually makes any sense as it is crossing boundry between values of ID.

159  data want ;

160    set have ;

161    by var_2 notsorted;

162    var_3=last.var_2;

163    put (id var_1 var_2 var_3) (3.);

164  run;

  1  1  1  0

  1  2  1  1

  1  3  0  0

  1  4  0  1

  1  5  1  0

  2  6  1  1

  2  7  0  0

  2  8  0  1

  2  9  1  0

  2 10  1  1

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 840 views
  • 6 likes
  • 3 in conversation