Help using Base SAS procedures

Concatenate text variables and numeric variables

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Concatenate text variables and numeric variables

I would like to know how to create the result as descripted below. Thank you!

data example;

input

ind $120

tri 2.

pop_ocup 12.0

trivaria 12.0

trivaria_pc 4.0

if tri > 0 then text1 = 'The '|| ind || 'increased in ' || trivaria ||' thousand people,' || trivaria_pc ||'% in relation to the last quarter.';

Result that I want:

The population increased in 2,853 thousand people, 2.3% in relation to the last quarter.


Accepted Solutions
Solution
‎06-02-2015 03:20 PM
Super User
Super User
Posts: 7,039

Re: Concatenate text variables and numeric variables

Posted in reply to William_Kratochwill

Why do you have both a SET statement and a CARDS statement?  Are you combining existing datasets or reading in-line data?


Why do you have multiple CARDS statements?  The first one will truncate the data step.  That is why the statements after it are generating errors since you cannot have an IF statement that is not part of a DATA step.


View solution in original post


All Replies
Super User
Super User
Posts: 7,039

Re: Concatenate text variables and numeric variables

Posted in reply to William_Kratochwill

catx(' ',.....)

data example;

length ind $120 trivaria trivaria_pc 8;

input ind -- trivaria_pc;

text1=catx(' ','The',ind,'inceased in',put(trivaria,comma20.)

      ,'thousand people,'

      ,cats(trivaria_pc,'%'),'in relation to the last quarter.');

put (_all_) (=/);

cards;

population 2853 2.3

;;;;

New Contributor
Posts: 3

Re: Concatenate text variables and numeric variables

Thank you,

But it is not running... Smiley Sad

this is the program:

data dados;

set

t1 t2 t3 t4 t5 t6;

trivar =  put(tri_varia_abs,commax12.0);

trivarp = Put(Tri_Varia_pc,commax12.1);

if ano = 2015 and indicadores ='a_pop_total' and tri = 'jan-fev-mar' and tri_situac = '*' and tri_varia_abs > 0 then

text1 = catx(' ','A ', indicadores, 'aumentou em ' , trivar,'mil pessoas, um aumento de ' , trivarp,'% em relação ao trimestre anterior.');

put (_all_) (=/);

cards;

if ano = 2015 and indicadores ='a_pop_total' and tri = 'jan-fev-mar' and  tri_situac = '*' and tri_varia_abs < 0 then text1 = catx(' ','A ', indicadores , 'diminuiu em ' ,  trivar,'mil pessoas, um aumento de ' , trivarp,'% em relação ao trimestre anterior.');

put (_all_) (=/);

cards;

if ano = 2015 and indicadores ='a_pop_total' and tri = 'jan-fev-mar' and  tri_situac ne '*'  then text1 = catx(' ','A ', indicadores, ' não apresentou variação estatísticamente significativa em relação ao trimestre anterior.');

put (_all_) (=/);

cards;

run;

and this is the Log:

26         if ano = 2015 and indicadores ='a_pop_total' andtri = 'jan-fev-mar' and  tri_situac = '*' and tri_varia_abs < 0 then

           __

           180

26       ! text1 = catx(' ','A ', indicadores , 'diminuiu em ' ,  trivar,'mil pessoas, um aumento de ' , trivarp,'% em relação ao

26       ! trimestre anterior.');

ERROR 180-322: Statement is not valid or it is used out of proper order.

27         put (_all_) (=/);

           ___

           180

ERROR 180-322: Statement is not valid or it is used out of proper order.

28         cards;

           _____

           180

ERROR 180-322: Statement is not valid or it is used out of proper order.

29         if ano = 2015 and indicadores ='a_pop_total' and tri = 'jan-fev-mar' and  tri_situac ne '*'  then text1 = catx(' ','A ',

           __

           180

29       ! indicadores, ' não apresentou variação estatísticamente significativa em relação ao trimestre anterior.');

ERROR 180-322: Statement is not valid or it is used out of proper order.

30         put (_all_) (=/);

           ___

           180

ERROR 180-322: Statement is not valid or it is used out of proper order.

31         cards;

           _____

           180

ERROR 180-322: Statement is not valid or it is used out of proper order.

32        

33         run;

Solution
‎06-02-2015 03:20 PM
Super User
Super User
Posts: 7,039

Re: Concatenate text variables and numeric variables

Posted in reply to William_Kratochwill

Why do you have both a SET statement and a CARDS statement?  Are you combining existing datasets or reading in-line data?


Why do you have multiple CARDS statements?  The first one will truncate the data step.  That is why the statements after it are generating errors since you cannot have an IF statement that is not part of a DATA step.


New Contributor
Posts: 3

Re: Concatenate text variables and numeric variables

Thank you!

There was also another mistake in the data. But now it is running perfectly.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 280 views
  • 2 likes
  • 2 in conversation