BookmarkSubscribeRSS Feed
Siddharth123
Obsidian | Level 7

Hi All,

I have a SAS dataset structured as :

Var1Var2Var3
A..
A..
A..
A116APR2010
A..
B..
B117MAY2010

B

..
B..

I want to create macro variables : first which counts the distinct number of values in Var1 (2 in this case - A &B) and for each value create a process which reads the data ONLY until Var2 = 1 for each value. Once this is done, then I would like to populate my VAR3 backwards, for example in above case it would be 16APR2010 for A and 17 May2010 for B so in total there should be 6 records in final output file.

Please help.

Kind regards

SK

1 REPLY 1
slchen
Lapis Lazuli | Level 10

data have;
input var1 $ var2 var3 :date9.;
format var3 date9.;
cards;
A    .    .
A    .    .
A    .    .
A    1    16APR2010
A    .    .
B    .    .
B    1    17MAY2010
B    .    .
B    .    .
;
run;

/*Distinct number of values in var1*/

proc sql;

select count(distinct var1) into: num from have;

quit;

%put #

data want;
do i=1 by 1 until(last.var1);
    set have;
    by var1;
    retain count;
    if var2=1 then count=i;
end;
do i=1 by 1 until (last.var1);
    set have;
    by var1;
    if i<=count then output;
    end;
format var3 date9.;
drop i count;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1016 views
  • 0 likes
  • 2 in conversation