BookmarkSubscribeRSS Feed
akberali67
Calcite | Level 5

Hi,

Is there a way to create variable names using string from an observation. This is where I got with the code:

DATA SASDATA.BAD1_REPAIRED;

SET SASDATA.BAD1;

COUNT + (-(LAG(FLAG) EQ 'LAST')*COUNT) + 1;

DO FIELD&S. = TOTAL_TEXT (S = COUNT); END;

RUN;

I am trying to do this

INPUT:

total_text count

abcdef      1

adfgh        2

LAST        3

OUTPUT:

total_text count field1      field2      field3

abcdef      1      abcdef

adfgh        2                    adfgh

LAST        3                                 LAST

Please help. Thanks again for your help.

-Akber.

6 REPLIES 6
akberali67
Calcite | Level 5

Need help please!!!!

rtritz
Calcite | Level 5

Akber, a proc transpose should do it.

data x;

input total_text $ count;

datalines;

abcdef  1

adfgh    2

LAST    3

;

run;

proc transpose data = x out=tx(drop=_name_) prefix=field;  

by total_text notsorted;  

id count; 

var total_text;

run;

HTH,

Rich

Ksharp
Super User

Here is array method.

data x;
input total_text $ count;
datalines;
abcdef  1
adfgh    2
LAST    3
;
run; 
proc sql;
 select max(count) into : n from x;
quit;
data want;
 set x;
 array field{&n} $ ;
 field{count}=total_text;
run;



Ksharp

Tom
Super User Tom
Super User

From your example it does not look like you want to create variable names, instead you want to put text into a matrix of variables.

The only thing you need to calculate before writing the code is the maximum number of counts.

Perhaps if you explained more about WHY you would want to do this you might get a better answer.

data have ;

  input total_text :$32. count @@;

cards;

abcdef 1 adfgh 2 LAST 3

run;

proc sql noprint;

  select max(count) into :max from HAVE ;

quit;

data want ;

  set have;

  array field (&max) $32 ;

  field(count)=total_text;

run;

proc print; run;

      total_

Obs     text     count    field1    field2    field3

1     abcdef      1      abcdef

2     adfgh       2                adfgh

3     LAST        3                           LAST

kuridisanjeev
Quartz | Level 8

Oh..

its looks very simple..

Great skill tom..

akberali67
Calcite | Level 5

Hi,

The reason why I want to do this is because these are actually parts of line but some data system is outputting them into different lines. So I want to retain until you have LAST in the data. So, what it will do is create one line in the final dataset. But this works and is quite simple.

Thanks again.

-Akber.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1070 views
  • 3 likes
  • 5 in conversation