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.

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