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

From the table below, if I want to make a variable that shows at least "1" in v1 as the same value,  

 

year

idv1
110
210
310
120
220
321
130
231
140
240
340

 

looks like this

 

yearidv1v2
1100
2100
3100
1201
2201
3211
1301
2311
1400
2400
340

0

 

How can I simply make a code?

Last time, I used first and last function, but it made too many datasets. 

I think coalesce in proc sql can do something, but I don't know how to do it...

Could you please help me?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

it's a straight forward proc sql 

 

data have;
input year id	v1;
cards;
1	1	0
2	1	0
3	1	0
1	2	0
2	2	0
3	2	1
1	3	0
2	3	1
1	4	0
2	4	0
3	4	0
;

proc sql;
create table want as
select *,max(v1) as v2
from have
group by id
order by id,year;
quit;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

it's a straight forward proc sql 

 

data have;
input year id	v1;
cards;
1	1	0
2	1	0
3	1	0
1	2	0
2	2	0
3	2	1
1	3	0
2	3	1
1	4	0
2	4	0
3	4	0
;

proc sql;
create table want as
select *,max(v1) as v2
from have
group by id
order by id,year;
quit;
asinusdk
Calcite | Level 5
Thank you very much. I'm learning from you a lot. Have a good day.
novinosrin
Tourmaline | Level 20

as usual some fun

 


data have;
input year id	v1;
cards;
1	1	0
2	1	0
3	1	0
1	2	0
2	2	0
3	2	1
1	3	0
2	3	1
1	4	0
2	4	0
3	4	0
;

data want;
merge have have(rename=(v1=_iorc_) where=(_iorc_=1) keep=id v1);
by id;
v2=_iorc_>.;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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