BookmarkSubscribeRSS Feed
carl_miles
Obsidian | Level 7

data MyDataset;

input id $4. 1L_flag $4. 2L_flag $4.;

datalines;

1 1L 0 

2 1L 2L

3 1L 0

4 1L 2L

5 1L 2L

;

run;

 

I want an output like 

id 1L_flag 2L_flag  1l_2l_flag

1       1L          0            1L

2      1L         2L           1L

2      1L         2L           2L

3      1L         0             1L

4     1L         2L           1L

4     1L         2L           2L

5      1L         2L           1L

5      1L         2L           2L


As we can see in the ouptut the flag made the patient level table to record level by flagging 
So If patients had 1l Only flag will give only 1L
if they 1L & 2L as well then they will have 2 records in the same flags 

[Note: In dataset I have multiple 1l and 2l and 3l .... flags 
and other columns as well with 25 columns

16, 456 rows ]

 

Could guys help to code and how can we do it?

2 REPLIES 2
andreas_lds
Jade | Level 19

Are the other columns expected to appear in the output? I don't think it is a good idea to name the last variable "1l_2l_flag" if the data has more flag-variables. You may want to post data that is closer to the data you have actually.  And please fix the names of your variables so that match the v7 rules - names start with a letter or with an underscore.

 

EDIT: and please re-check the code, it doesn't work as expected due to formatted input.

andreas_lds
Jade | Level 19

I have renamed the flag-variables, if you insist on using bad names, you will have to edit the array statement.

data work.have;
   input id $ flag_L1 $ flag_L2 $;
   datalines;
1 1L 0 
2 1L 2L
3 1L 0
4 1L 2L
5 1L 2L
;

data want;
   set have;

   length flag $ 4;
   array flags flag_L:;

   do i = 1 to dim(flags);
      if flags[i] ^= '0' then do;
         flag = flags[i];
         output;
      end;
   end;

   drop i;
run;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 600 views
  • 0 likes
  • 2 in conversation