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

Please help with this
how can you transfer data from situation 1 to situation 2?
data situation 1:
column A : A1 = 1 ; A2 = 2 ; A3=3
column B : B1 = 11 ; B2 = 12 ; B3 = 13
column C : C1 = allo , okay ; C2 = ; C3 = z , d , h
data situation 2:
column A : A1 = 1;  A2 = 1;  A3 = 2;  A4 = 3;   A5 = 3;  A6 = 3
column B : B1 = 11; B2 = 11; B3 = 12; B4 = 13; B5 = 13; B6 = 13
column C : C1 = allo;  C2 = okay;  C3 = ;  C4 = z ;  C5 = d;  B6 = h

note that C2 is when situation 1 is empty and C3 is when situation 2 is empty
so data input is situation 1 and data output is situation 2

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Someone appears to be thinking like a spreadsheet. If A1 is supposed to be the value of variable A in the first observation then you have a data set that looks like:

data have;
   input a b c:$15.;
datalines;
1 11 allo,okay
2 12 .
3 13 z,d,h
;


ure please attempt to provide existing data in the form of a working data step pasted into a text box opened on the forum using the </> icon that appears above the main message window. The text box is important because the forum software will reformat pasted text and may result in code that will not run.

The above assumes none of your C values have spaces in them and a space can be used to delimit the data for reading with list input and assumes that A and B are numeric values since you did not specify in any way whether they are numeric or character.

 

Such can be done with :

data want (rename=(cword=c));
  set have;
  length cword $ 15;
  words=countw(c,',');
  if words=0 then output;
  else do i=1 to words;
   cword = scan(c,i,',');
   output;
  end;
  drop i words c;
run;

Because we need the entire value of C someplace and you want different values apparently in the output we will need to rename the C at some time. The above code renames the single value variable on the output set to the name of C.

The OUTPUT instruction writes to the output set when encountered. So you can specify conditions and values when using such. In this case we determine of the C value has any elements counting values separated by commas. If none, then write out immediately. Otherwise extract each "word" using the SCAN function and then write out one at a time. The other variable values, A and B are not manipulated so the starting value gets written out repeatedly for value in C.

View solution in original post

2 REPLIES 2
ballardw
Super User

Someone appears to be thinking like a spreadsheet. If A1 is supposed to be the value of variable A in the first observation then you have a data set that looks like:

data have;
   input a b c:$15.;
datalines;
1 11 allo,okay
2 12 .
3 13 z,d,h
;


ure please attempt to provide existing data in the form of a working data step pasted into a text box opened on the forum using the </> icon that appears above the main message window. The text box is important because the forum software will reformat pasted text and may result in code that will not run.

The above assumes none of your C values have spaces in them and a space can be used to delimit the data for reading with list input and assumes that A and B are numeric values since you did not specify in any way whether they are numeric or character.

 

Such can be done with :

data want (rename=(cword=c));
  set have;
  length cword $ 15;
  words=countw(c,',');
  if words=0 then output;
  else do i=1 to words;
   cword = scan(c,i,',');
   output;
  end;
  drop i words c;
run;

Because we need the entire value of C someplace and you want different values apparently in the output we will need to rename the C at some time. The above code renames the single value variable on the output set to the name of C.

The OUTPUT instruction writes to the output set when encountered. So you can specify conditions and values when using such. In this case we determine of the C value has any elements counting values separated by commas. If none, then write out immediately. Otherwise extract each "word" using the SCAN function and then write out one at a time. The other variable values, A and B are not manipulated so the starting value gets written out repeatedly for value in C.

melassiri
Fluorite | Level 6

thank you for your help. I appreciate that

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 462 views
  • 2 likes
  • 2 in conversation