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

hello, i'm trying to create time-dependent variable, which means that the square of 'visiting number of each clinics' changes over time.

 

1) square of 'visiting number of each clinics means

 

1234.jpg

 

ni is visiting number of 'i' clinic.
M is number of clinic.

 

and if i go to clinic 'a' and clinic 'b' in following sequence

 

a a a b b

 

... then total square is 9+4 = 13.

 

2) and i want make this 'total square' in time-dependent variable.

 

this is raw data.

 

id clinic

1 a
1 a
1 b
1 b
1 a
1 c
2 a
2 b
2 c
3 a
3 a
3 a
3 b
3 a

 

...and i want to create dataset like this.

 

id clinic totalsquare

 

1 a 1
1 a 4
1 b 5 *4+1*
1 b 8 *4+4*
1 a 13 *4+9*
1 c 14 *1+4+9*
2 a 1
2 b 2 *1+1*
2 c 3 *1+1+1*
3 a 1
3 a 4
3 a 9
3 b 10 *1+9*
3 a 17 *1+16*

 

thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

This is an excellent homework assignment for understanding BY variables, and retaining values.

 

What have you tried so far?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
mkeintz
PROC Star

This is an excellent homework assignment for understanding BY variables, and retaining values.

 

What have you tried so far?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
km0927
Obsidian | Level 7

1. in retain variable, i don't know how to replace retained value to another.

 

example

 

1 a 1

1 b 1+1

1 c 1+1+1

1 c 1+1+4

 

if i use retain variable by group, the last row become '1+1+1+4'(third '1' is retained from third row)

 

2. and when i use 'retain' by id and clinic, then i have to use 'proc sort' by id and clinic.

 

i want to make cumulative square sum in time order.

mkeintz
PROC Star

I'm not sure why you marked my comment as a solution for this topic.  However, if you show even your code for reading in the original variables, without attempting the cumulative sum-of-squares, then you can have a start at getting help.

 

The variable to be "retained" is the cumulative sum of squares, which you (1) want to update with every incoming observation, and (2) you want to reset every time you begin a new id.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!
How to Concatenate Values

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.

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
  • 1026 views
  • 0 likes
  • 2 in conversation