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

Hi,

I have a SAS dataset as follows:

data source;

input id cols $10.;

cards;

1 gender,age

2 address

;

run;

Now, I would like to transpose those values in COLS column which is currently separated by a comma. The target dataset is as follows:

data target;

input id cols $;

cards;

1 gender

1 age

2 address

;

run;

How will I be able to achieve the target dataset?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

data source;

input id cols $10.;

cards;

1 gender,age

2 address

;

run;

data source2;

  set source;

  retain id;

  do i = 1 to countw(cols,',');

new_cols=scan(cols,i,',');

if new_cols ne '';

output;

end;

drop i;

run;

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

Please try

data source;

input id cols $10.;

cards;

1 gender,age

2 address

;

run;

data source2;

  set source;

  retain id;

  do i = 1 to 2;

new_cols=scan(cols,i,',');

if new_cols ne '';

output;

end;

drop i;

run;

Thanks,

Jag

Thanks,
Jag
Ksharp
Super User

data source;

input id cols $10.;

cards;

1 gender,age

2 address

;

run;

data source2;

  set source;

  retain id;

  do i = 1 to countw(cols,',');

new_cols=scan(cols,i,',');

if new_cols ne '';

output;

end;

drop i;

run;

angeliquec
Quartz | Level 8

thank you xia keshan!

Kurt_Bremser
Super User

Interesting. From the SAS documentation for the subsetting if:

If the expression is false (its value is 0 or missing), no further statements are processed for that observation or record, the current observation is not written to the data set, and the remaining program statements in the DATA step are not executed. SAS immediately returns to the beginning of the DATA step because the subsetting IF statement does not require additional statements to stop processing observations.

(emphasis by me)

This would imply that a value for cols of "Age,,Gender" would lead to "Gender" being dropped, but that does actually not happen. SAS only exits the do loop.

Learning never stops.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 2085 views
  • 2 likes
  • 4 in conversation