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.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 376 views
  • 2 likes
  • 4 in conversation