BookmarkSubscribeRSS Feed
pfvk
Fluorite | Level 6

Hi, I am a beginner in SAS

I have a dataset like below

year      agep      N

2000       3          4

2000       5          6

2000       6          7

 

There are 6 age groups in my dataset. I would like to know if I can via SAS code finish the below table automatically.

year      agep      N

2000       1          0

2000       2          0

2000       3          4

2000       4          0

2000       5          6

2000       6          7

 

Thanks in advance!




3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Is this your actual data or sample data that resembles your actual data?

 

Is the agep variable always values from 1 to 6 or can it vary?

PaigeMiller
Diamond | Level 26

How would the programmer (in this case you or me) know that the values of N start at zero and continue up to 6 (and not go above 6)?

 

Do you want code that works on this exact data set, or is the problem more general and has to work on similar data sets with different values of N?

 

Are you trying to create a table or report? Or are you trying to create a data set?

 

 

--
Paige Miller
Patrick
Opal | Level 21

Below one way to get there.

data have;
 input year agep N;
 datalines;
2000 3 4
2000 5 6
2000 6 7
;

data classdata;
  year=2000;
  n=0;
  do agep=1 to 6;
    output;
  end;
run;

data want;
  merge classdata have;
  by year agep;
run;

proc print data=want;
  var year agep n;
run;

Patrick_0-1672659101376.png

With a merge "last wins": If there is a matching row from table have (listed last in the merge statement) then the value for agep will be taken from that table (=overwriting the value from classdata). If there is no matching row then agep will have the value from classdata.

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 732 views
  • 2 likes
  • 4 in conversation