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

Any help would be greatly appreciated it. I am trying to create a variable let's say called "NewVar" that would indicate the "time at which someone" quit even though it's not a date. For example I have data that looks like this and the last column shows what the new variable would be: 

ObsValue_S1Value_S2Value_S3Value_S4Value_S5Value_S6Value_S7Value_S8Value_S9Value_S10Value_S11Value_S12Value_S13Value_S14Value_S15Value_S16NewVar
1188...............1
2167...............1
3233236..............2
4278280275275276...........5
5253252  346 248......23523423416
6213215215218217212212.........7
7281 279281281 280........27916
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
infile cards truncover;
input Value_S1	Value_S2	Value_S3	Value_S4	Value_S5	Value_S6	Value_S7	Value_S8	Value_S9	Value_S10	Value_S11	Value_S12	Value_S13	Value_S14	Value_S15	Value_S16;
cards;
188	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.
167	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.
233	236	.	.	.	.	.	.	.	.	.	.	.	.	.	.
278	280	275	275	276	.	.	.	.	.	.	.	.	.	.	.
253	252	. .		346	.	248	.	.	.	.	.	.	235	234	234
213	215	215	218	217	212	212	.	.	.	.	.	.	.	.	.
281	.	279	281	281	.	280	.	.	.	.	.	.	.	.	279
;

data want;
set have;
array j(*)Value_S16-Value_S1;
_iorc_=coalesce(of j(*));
new_var=dim(j)-whichn(_iorc_,of j(*))+1;
run;

View solution in original post

9 REPLIES 9
novinosrin
Tourmaline | Level 20

data have;
infile cards truncover;
input Value_S1	Value_S2	Value_S3	Value_S4	Value_S5	Value_S6	Value_S7	Value_S8	Value_S9	Value_S10	Value_S11	Value_S12	Value_S13	Value_S14	Value_S15	Value_S16;
cards;
188	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.
167	.	.	.	.	.	.	.	.	.	.	.	.	.	.	.
233	236	.	.	.	.	.	.	.	.	.	.	.	.	.	.
278	280	275	275	276	.	.	.	.	.	.	.	.	.	.	.
253	252	. .		346	.	248	.	.	.	.	.	.	235	234	234
213	215	215	218	217	212	212	.	.	.	.	.	.	.	.	.
281	.	279	281	281	.	280	.	.	.	.	.	.	.	.	279
;

data want;
set have;
array j(*)Value_S16-Value_S1;
_iorc_=coalesce(of j(*));
new_var=dim(j)-whichn(_iorc_,of j(*))+1;
run;
SmithCJGVSU
Obsidian | Level 7

@novinosrin, nice use of WHICHN, plus I didn't realize you can use an array reference as an "OF LIST". You win the day my friend.

127
Fluorite | Level 6 127
Fluorite | Level 6

works beautifully, thank you and others for this contribution!!!

wadams
Calcite | Level 5

Hi @novinosrin - How would the syntax change if the user wants to retain the value of the cell rather than the name of the column.  For example:

Subject 1 = 188

Subject 2 = 167

Subject 3 = 236

Subject 4 = 276

etc.

 

Thank you!

Bill

novinosrin
Tourmaline | Level 20

Hi @wadams  Bill, Sorry in the middle of a meeting at work and so a quick one assuming I understood your requirement-

data want;
  set have;
  array j(*)Value_S16-Value_S1;
  array k(*) Value_S1-Value_S16;*Compile another array Bill's additional request;
  _iorc_=coalesce(of j(*));
  new_var=dim(j)-whichn(_iorc_,of j(*))+1;
  new_value=k(new_var);*Compute another assignment for  Bill's additional request;
run;

Please see if the above suffices. If not, i will take a look in the evening EDT.

wadams
Calcite | Level 5

This worked perfectly.  Thank you so much @novinosrin!

s_lassen
Meteorite | Level 14

While the solution suggested by @novinosrin is ingenious (and it works), I would prefer something a bit more obvious:

data want;
  set have;
  array values Value_S1-Value_S16;
  do NewVar=dim(values) to 1 by -1 while(missing(values(NewVar)));
    end;
run;

 

SmithCJGVSU
Obsidian | Level 7
I like this too! Two great solutions.
novinosrin
Tourmaline | Level 20

Thank you @SmithCJGVSU and @s_lassen  for the very kind words. Feels very encouraging to pursue harder and further. Pleasant as it was to see the first thing in the morning waking up. 🙂

 

 

Hi  @tugtrog   As you know I share pretty stuff ,here is another one. @Cruise , Since, your attitude is great as that of @tugfrog  in honing further, I am pulling you in

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 9 replies
  • 3717 views
  • 8 likes
  • 5 in conversation