SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. 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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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