Consider my data as below,
ID | OBS | Q2A1 | Q2B1 | Q2C1 |
10091 | 1 | Upper Trunk | Anterior | Left |
10091 | 2 | Upper Trunk | Posterior | Left |
What i want is:
Result | FLAG VARIABLE |
Upper Trunk | 1 |
Anterior | 1 |
Left | 1 |
Upper Trunk | 2 |
Posterior | 2 |
Left | 2 |
I am able to derive the Result column but accordingly for 1st OBS, I need a flag variable assigned. Please help here, I am a novice programmer.
Please try to provide data in the form of data step code as below. It will make responses much more usable and avoid things like variable type questions.
For what you show:
data have; infile datalines dlm=','; informat id $5. obs 5. Q2A1 Q2B1 Q2C1 $15.; input ID OBS Q2A1 Q2B1 Q2C1; datalines; 10091,1,Upper Trunk,Anterior,Left 10091,2,Upper Trunk,Posterior,Left ; proc transpose data=have out=want (rename=(obs=flagvariable) drop=_name_) ; by obs; var Q2A1 Q2B1 Q2C1; run;
However the Proc transpose assumes the data is sorted by OBS, that there are not multiple duplicate values of any of the Q variables. I have interpreted "accordingly for 1st OBS" to mean "use the value of the OBS variable as the flag". If that is not what is intended then you need to provide more rules.
Note that the code is posted into a text box opened using the </> icon at the top of the message window. The main message windows on this forum will reformat text pasted into the window and may result in code that does not run properly when copied out and pasted into the SAS editor.
Please try to provide data in the form of data step code as below. It will make responses much more usable and avoid things like variable type questions.
For what you show:
data have; infile datalines dlm=','; informat id $5. obs 5. Q2A1 Q2B1 Q2C1 $15.; input ID OBS Q2A1 Q2B1 Q2C1; datalines; 10091,1,Upper Trunk,Anterior,Left 10091,2,Upper Trunk,Posterior,Left ; proc transpose data=have out=want (rename=(obs=flagvariable) drop=_name_) ; by obs; var Q2A1 Q2B1 Q2C1; run;
However the Proc transpose assumes the data is sorted by OBS, that there are not multiple duplicate values of any of the Q variables. I have interpreted "accordingly for 1st OBS" to mean "use the value of the OBS variable as the flag". If that is not what is intended then you need to provide more rules.
Note that the code is posted into a text box opened using the </> icon at the top of the message window. The main message windows on this forum will reformat text pasted into the window and may result in code that does not run properly when copied out and pasted into the SAS editor.
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!
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.
Ready to level-up your skills? Choose your own adventure.