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

Hello,

I wonder if someone could help me? I try to make a table which inherits the group information from previous value. Thank you.

Input:

groupid
group11
 2
 3
group24
 5
group36

 

Ouput:

groupid
group11
group22
group33
group24
group35
group36
1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Either I don't understand or your example wasn't right. Are you trying to do the following:

 

data have;
  infile cards dlm=',' truncover;
  input group $ id;
  cards;
group1,1
 ,2
 ,3
group2,4
 ,5
group3,6
;

proc sort data=have;
  by descending id;
run;

data want (drop=hold);
  set have;
  retain hold;
  if not missing(group) then hold=group;
  else group=hold;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Either I don't understand or your example wasn't right. Are you trying to do the following:

 

data have;
  infile cards dlm=',' truncover;
  input group $ id;
  cards;
group1,1
 ,2
 ,3
group2,4
 ,5
group3,6
;

proc sort data=have;
  by descending id;
run;

data want (drop=hold);
  set have;
  retain hold;
  if not missing(group) then hold=group;
  else group=hold;
run;

Art, CEO, AnalystFinder.com

 

lionking19063
Fluorite | Level 6

Thank you so much. It worked.

Tom
Super User Tom
Super User

You should just be able to use a retained variable.

data want ;
  set have;
  hold = coalescec(group,hold);
  group = coalescec(group,hold);
  retain hold;
  drop hold;
run;

Unfortunately SAS sees the first reference to HOLD as being the reference inside the function call instead of the result of assignment statement and defaults HOLD to numeric.  So you need to add a LENGTH statement to define HOLD.

 

Or you could use this trick to make a copy of GROUP with the name of HOLD. That way you do not need to know the length of GROUP in advance.  It will also mark the variable as retained since it is coming from a dataset, so the RETAIN statement is not needed.

 

data want ;
   set have have (obs=0 rename=(group=hold));
   hold = coalescec(group,hold);
   group = coalescec(group,hold);
   drop hold;
run;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 821 views
  • 0 likes
  • 3 in conversation