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

Hello everyone, I need help to find a procedure to create a new variable "long_name", which should be populated according to the relation between the variable "code" and the variable "name", the code variable is a drill down of the geographical location in the "name" variable.

 

Data input:

CODENAME
A1EUROPE
A11GERMANY
A111BERLIN
A12FRANCE
A121NICE
A122LYON
A123PARIS
A2AFRICA
A21NIGERIA
A211LAGOS

 

Desired output:

 

CODENAMELONG_NAME
A1EUROPEEUROPE
A11GERMANYEUROPE_GERMANY
A111BERLINEUROPE_GERMANY_BERLIN
A12FRANCEEUROPE_FRANCE
A121NICEEUROPE_FRANCE_NICE
A122LYONEUROPE_FRANCE_LYON
A123PARISEUROPE_FRANCE_PARIS
A2AFRICAAFRICA
A21NIGERIAAFRICA_NIGERIA
A211LAGOSAFRICA_NIGERIA_LAGOS

 

Thank you so much in advance.

jmrc

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

Look to use the PROC FORMAT CNTLIN and datastep

 

/* Create a CNTLIN dataset for PROC FORMAT */
/* See https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p0owa4ftikc2ekn1q0rmpulg86cx.htm */

data have ;
	fmtname="$Place" ;
	input start $ label $ ;
	cards ;
A1 Europe
A11 Germany
A111 Berlin
A12 France
A121 Nice
A122 Lyon
A123 Paris
A2 Africa
A21 Nigeria
A211 Lagos
;

/* Create $Place format using the CNTLIN dataset */
proc format cntlin=have ;
run ;

data want ;
	length longName $40. ;
	set have ;
	/* Determine length of variable start */
	strLen=length(start) ;
	/* Loop through start variable to build longName */
	do i=2 to strLen ;
		if i=2 then
			/* First 2 characters of start determines the continent */
			longName=putc(substr(start,1,i),"place.") ;
		else 
			/* 3rd, 4th characters will add country and city */
			longName=trim(longName)||"_"||putc(substr(start,1,i),"place.") ;
		put start= longName= ;
	end ;
	/* write to want dataset */
	output ;
run ;

View solution in original post

2 REPLIES 2
AMSAS
SAS Super FREQ

Look to use the PROC FORMAT CNTLIN and datastep

 

/* Create a CNTLIN dataset for PROC FORMAT */
/* See https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p0owa4ftikc2ekn1q0rmpulg86cx.htm */

data have ;
	fmtname="$Place" ;
	input start $ label $ ;
	cards ;
A1 Europe
A11 Germany
A111 Berlin
A12 France
A121 Nice
A122 Lyon
A123 Paris
A2 Africa
A21 Nigeria
A211 Lagos
;

/* Create $Place format using the CNTLIN dataset */
proc format cntlin=have ;
run ;

data want ;
	length longName $40. ;
	set have ;
	/* Determine length of variable start */
	strLen=length(start) ;
	/* Loop through start variable to build longName */
	do i=2 to strLen ;
		if i=2 then
			/* First 2 characters of start determines the continent */
			longName=putc(substr(start,1,i),"place.") ;
		else 
			/* 3rd, 4th characters will add country and city */
			longName=trim(longName)||"_"||putc(substr(start,1,i),"place.") ;
		put start= longName= ;
	end ;
	/* write to want dataset */
	output ;
run ;

jmrc
Calcite | Level 5

AMSAS, worked perfectly. Thank you so much.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 294 views
  • 1 like
  • 2 in conversation